import json
j = {
"employee":
[
{"id": 111, "name": "Mike"},
{"id": 222, "name": "Nancy"}
]
}
print(j)
print("###########")
#Note that s is added when reading and writing in python
print(json.dumps(j))
a = json.dumps(j)
print('@@@@@@@@@@')
print(json.loads(a))
#No s when reading or writing files
with open('test.json', 'w') as f:
json.dump(j, f)
print('###########')
with open('test.json', 'r') as f:
print(json.load(f))
output:
{'employee': [{'id': 111, 'name': 'Mike'}, {'id': 222, 'name': 'Nancy'}]}
###########
{"employee": [{"id": 111, "name": "Mike"}, {"id": 222, "name": "Nancy"}]}
@@@@@@@@@@
{'employee': [{'id': 111, 'name': 'Mike'}, {'id': 222, 'name': 'Nancy'}]}
###########
{'employee': [{'id': 111, 'name': 'Mike'}, {'id': 222, 'name': 'Nancy'}]}