read.json file :
{
"Username" : "admin",
"Password" : "admin",
"Iterations" : 5,
"Decimal" : 5.5,
"tags" : ["hello", "bye"],
"Value" : 5
}
program.py File:
import json
with open('read.json') as data_file:
data = json.load(data_file)
data = str(data)
data.replace("'",'""',10)
f = open("write.json", "w")
f.write(data)
write.json file :
{'Username': 'admin', 'Password': 'admin', 'Iterations': 5, 'Decimal': 5.5, 'tags': ["hello", "bye"], 'Value': 5}
What I want to achieve :
- Read JSON data from read.json File
- Parse and modify some values from the JSON in my program
- Write to another write.json file (In JSON Format)
There are no errors in my code, but the write.json does not contain the values in double quotes(""), it rather as the values wrapped in single quotes making it not a proper JSON format.
What change needs to be done to make the write.json file to contain proper JSON format and also 'pretty-write' to write.json file.
data(which will be adict) as you receive it from thejson.load()call and write it back to a file usingjson.dump(). No need to usestr()inbetween.json.load(data)tojson.loads(data)loads()takes str not file.json