Apr-21-2022, 04:43 PM
(Apr-21-2022, 03:34 PM)deanhystad Wrote: 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.
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 } ]
Thanks a lot.
This output format isn't enough compact.
I prefer this one :
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}
]
Is it allowed for json format ?
If yes, I will add code to obtain this format (not with json library).
Is there other format to obtain this type of output? I can make my format but I would like to use known format.