I have a js file abc.js containing a JSON object as below:
export default{
"Name": "James",
"Age": "21"
}
I have a python dictionary dict with the value:
{"Country":"Italy"}
How can I append dict to abc.js such that it becomes:
export default{
"Name": "James",
"Age": "21",
"Country":"Italy"
}
I tried the below approach:
with open("abc.js", "w") as file:
json.dump(dict, file)
I’m aware that this will replace the whole file but is there any way to append while also keeping the export default{} ?
2
Answers
Remove
export default
when reading the file, and add it back when writing.You can also extract JSON data from your js file, merge it with your Python dictionary, and write it back to the file.