I have the JSON variable var
with the following content:
[{"name":"name0", "surname":"surname01", "surname2":"surname02", "number": 0}, {"name":"name1", "surname":"surname11", "surname2":"surname12", "number": 1}]
And I want to write in a JSON file just the name
and the number
fields.
Currently, I am doing it the following way:
f.write('[')
for a, i in enumerate(var):
f.write('{"name":' + i['name'] + ', "number":' + i['number'] + '}')
if(a==0):
f.write(',')
f.write(']')
f.close()
Is there a better way to write to a file a subset of the JSON fields in Python?