import json
with open('result.json', 'w') as fp:
json.dump(sample, fp)
This is an easier way to do it.
In the second line of code the file result.json gets created and opened as the variable fp.
In the third line your dict sample gets written into the result.json!
import json
with open('result.json', 'w') as fp:
json.dump(sample, fp)
This is an easier way to do it.
In the second line of code the file result.json gets created and opened as the variable fp.
In the third line your dict sample gets written into the result.json!
Combine the answer of @mgilson and @gnibbler, I found what I need was this:
d = {
"name": "interpolator",
"children": [{
'name': key,
"size": value
} for key, value in sample.items()]
}
j = json.dumps(d, indent=4)
with open('sample.json', 'w') as f:
print >> f, j
It this way, I got a pretty-print json file.
The tricks print >> f, j is found from here:
http://www.anthonydebarros.com/2012/03/11/generate-json-from-sql-using-python/
convert list of dictionary to list of json objects
Store data as Python or JSON file?
In the debugger I often have to convert python dict to json is there a better way than evaluate expression with json.dumps()?
How would you use python to create JSON files?
Videos
Hello I currently learning json in python i want to append a dictionary in a json file ontop of existing ones but every time i do this i get this error in VS-Code:
End of file expected.
Can somebody help me?
Here is the Code:
dict = {
"data1" : data3,
"data2" : data4
}
data = json.dumps(dict)
with open("index.json" , "a") as file:
json.dump(data , file)
How to convert list of dictionary to list of json objects?
input = [{'a':'b'},{'c':'d'}]
expectedOutput = [{"a":"b"},{"c":"d"}]
I tried following but couldn't get expected result
-
json.dumps(input)gives string'[{"a":"b"},{"c":"d"}]' -
Iterate through dictionary and convert each dict to json object gives
['{"a":"b"}','{"c":"d"}']