1import json
2
3x = '{ "name":"John", "age":30, "city":"New York"}'
4y = json.loads(x)
5
6print(y["age"])
1import json
2
3# some JSON:
4x = '{ "name":"John", "age":30, "city":"New York"}'
5
6# parse x:
7y = json.loads(x)
8
9# the result is a Python dictionary:
10print(y["age"])
1# a Python object (dict):
2x = {
3 "name": "John",
4 "age": 30,
5 "city": "New York"
6}
7
8# convert into JSON:
9y = json.dumps(x)
1
2import json
3
4with open('path_to_file/person.json') as f:
5 data = json.load(f)
6
7# Output: {'name': 'Bob', 'languages': ['English', 'Fench']}
8print(data)
9
1>>> import json
2>>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
3['foo', {'bar': ['baz', None, 1.0, 2]}]
4>>> json.loads('"\\"foo\\bar"')
5'"foo\x08ar'
6>>> from io import StringIO
7>>> io = StringIO('["streaming API"]')
8>>> json.load(io)
9['streaming API']