'unable to replace a part of json value using fs javascript
this is my js code
let content019 = JSON.parse(require("fs").readFileSync("./settings.json"));
// edit or add property
content019.api.client.locations[1] = 999999999999;
//write file
fs.writeFileSync('settings.json', JSON.stringify(content019,null ,2));
i tried using content019.api.client.locations[1] but it changed the inner part to "1": "999....
this is a part of my json file
"1": {
"name": "Germany",
"type": null
},
"2": {
"name": "Singapore",
"type": null
}
},
i want it to only change "1": { to "999999999999": {
even tried content019.api.client.locations.1, didnt work. received error unexpected number
Solution 1:[1]
you have to use a string, not a number index. Since your json is invalid, I can only assume that the first part is ok, so you can try
content019.api.client.locations["1"] = 999999999999;
or maybe
content019.api.client.locations["999999"] = content019.api.client.locations["1"];
delete content019.api.client.locations["1"];
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Serge |
