Reputation: 59
I have the a JSON file and a need to add a prefix variable in some fields, but did not work pass the variable using $variable, {variable}, etc.
My Original JSON file is shown below.
{
"SETUP": {
"mission": "P:\Project",
"mission_type": "0",
"fortnight": "25",
"tide": "0"
},
"MISSION": {
"INPUT": {
"arms": "\cp\arms.ini",
},
}
I need to add the SETUP.mission prefix to the field Mission.INPUT.arms
{
"SETUP": {
"mission": "P:\Project",
"mission_type": "0",
"fortnight": "25",
"tide": "0"
},
"MISSION": {
"INPUT": {
"arms": SETUP.mission + "\cp\arms.ini",
},
}
MISSION.INPUT.arms = "P:\Project\cp\arms.ini"
Thank you
Upvotes: 0
Views: 431
Reputation: 855
JSON doesn't have a concept of variables. What you need to do is to load it on memory, modify it and save back to the file if needed. Here's an example of doing it in Node.js:
const fs = require('fs');
const path = require('path');
const json = require('./path/to/json/file.json');
json.MISSION.INPUT.arms = path.join(json.SETUP.mission, json.MISSION.INPUT.arms);
fs.writeFileSync('./path/to/json/file.json', JSON.stringify(json));
Upvotes: 1