Apr-21-2022, 03:34 PM
Load json, modify and dump is the correct method. You do not want to append the file.
You should not worry about the format of your json data. Json files are easy to read, but are not designed to be human readable. You can make the file a little prettier using the indent argument.
You should not worry about the format of your json data. Json files are easy to read, but are not designed to be human readable. You can make the file a little prettier using the indent argument.
import json data = [ {"surname" : "xxx", "forename" : "jean", "age" : 24}, {"surname" : "yyy", "forename" : "paul", "age" : 31}, {"surname" : "zzz", "forename" : "julia", "age" : 28}, {"surname" : "aaa", "forename" : "Paul", "age" : 40} ] with open("test.json", "w") as outfile: json.dump(data, outfile, indent=4)
Output:[
{
"surname": "xxx",
"forename": "jean",
"age": 24
},
{
"surname": "yyy",
"forename": "paul",
"age": 31
},
{
"surname": "zzz",
"forename": "julia",
"age": 28
},
{
"surname": "aaa",
"forename": "Paul",
"age": 40
}
]