'Replace the contents in multiple JSON files based on their file name?
How to replace the contents in multiple JSON files based on their file name? I have a certain number of description files and I need to sort their contents by file name. For ex. files numbered 1.json, 2.json, ... 10.json All files inside have the same object names, but different numbers
{
"name": "Garlic 8",
"position": 8,
"description": "Only for stuff.",
"external": "row five",
"image": "buffer/8.png",
"basic": [
{
"type": "row",
"value": "one"
},
{
"color": "pure",
"value": "no"
}
]
}
How to replace numbers in all files with numbers from file names? (i.e. the numbers inside the file must match the file number)
const basePath = process.cwd();
const fs = require("fs");
const {baseUri, namePrefix,} = require(`${basePath}/src/config.js`);
for (
let i = 1;
i <= 10;
i++
)
{
let rawdata = fs.readFileSync(`${basePath}/rename/json/${i}.json`);
let data = JSON.parse(rawdata);
var originalMsg = JSON.stringify(data);
data.each(function(item) {
{
item.name = `${namePrefix} #${i}`;
item.position = `${i}`;
item.image = `${baseUri}/${i}.png`;
}
});
console.log(originalMsg)
console.log(data)
But typeError: data.each is not a function
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
