How can I replace the tokens __fruit_type__, __clothing_type__, __fitness_equipment__, __meditation_app__
in collection.js with values from values.js
?
I am trying to achieve this with the string.replace() Method
collection.js
{
"collection" : [
{
"fruit": "__fruit_type__",
"clothings":{
"item": "__clothing_type__}"
}
},
{
"fitness": "__fitness_equipment__",
"mindfulness": "app called __meditation_app__"
}
]
}
values.js
{
"clothing_type": "winter",
"fruit_type": "apple",
"fitness_equipment": "treadmill",
"meditation_app": "calm"
}
replace.js
const fs = require("fs").promises;
async function dataReader(filePath, data) {
const result = await fs.readFile(filePath);
try {
return JSON.parse(result);
} catch (e) {
console.error(e);
}
}
//read values
async () => {
const value = await dataReader("./values.json");
const clothing_type = value.clothing_type;
const fruit_type = value.fruit_type;
const fitness_equipment = value.fitness_equipment;
const meditation_app = value.meditation_app;
//read collection.json
const data = await jsonReader("./collections.json");
//replace tokens in `collection.js` with `values.js`
};
CodePudding user response:
USING REGEX !!
- Now you have a collection.json (like a template) file.
- And values.json (a filler for the template), which has a set of values that will be placed in the template (collection.json).
- We assume that keys of values.json file are the fillers in collection.json
- if a key in values.json is
clothing_type
then the placeholder is__clothing_type__
- We can now iterate through the keys of values.json and look for a match in the collection.json with two underscores in the prefix and suffix
///json will be parsed after filling the value
/// for this example i'll store the both the json files in varaible
const collections = `{
"collection" : [
{
"fruit": "__fruit_type__",
"clothings":{
"item": "__clothing_type__}"
}
},
{
"fitness": "__fitness_equipment__",
"mindfulness": "app called __meditation_app__"
}
]
}`
/// naming the values.json as fillers as this may confuse with key,values in object
const fillers = `{
"clothing_type": "winter",
"fruit_type": "apple",
"fitness_equipment": "treadmill",
"meditation_app": "calm"
}`
function replaceFiller(template_collections,fillers){
/// to loop through the keys of fillers or values.json we have parse this fisrt
const fillersObj =JSON.parse(fillers);
console.log(fillersObj);
//// we dont want to modify the argument varaible "template_collection"
/// so we will use a variable to store the template collection
/// and mutate them arbitrarily
let resultJson = template_collections;
/// looping through the keys of fillersObj
for(let fillerKey of Object.keys(fillersObj)){
/// here the filler key will be "clothing_type" ,"fruit_type" on every iteration
// findig a match for the current key(fillerKey) with underscores in the collections.json and replacing them with the current keys value in fillersObj
resultJson = resultJson.replace(`__${fillerKey}__`,fillersObj[fillerKey]);
console.log(fillerKey,fillersObj[fillerKey]);
}
return JSON.parse(resultJson);
}
console.log(replaceFiller(collections,fillers));
Caveats
- Values. json folder should never consist of nested objects.
- I have also had the same task with nested objects as fillers; I had to write a lot of code to flatten the objects and then replace tokens.
- This code is fine for simple cases like this. If you are going to handle large objects, then use this library jsonpath_object_transform