[PYTHON] json

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'}]}

Recommended Posts

json
json
JSON formatting
Python #JSON
JSON conversion class
Handling json in python
json parse with gdb
How to read JSON
What is JSON? .. [Note]
Returns json in Pyramid
Convert json to excel