1import json
2
3uglyjson = '{"firstnam":"James","surname":"Bond","mobile":["007-700-007","001-007-007-0007"]}'
4
5#json.load method converts JSON string to Python Object
6parsed = json.loads(uglyjson)
7
8print(json.dumps(parsed, indent=2, sort_keys=True))
1>>> import json
2>>>
3>>> your_json = '["foo", {"bar":["baz", null, 1.0, 2]}]'
4>>> parsed = json.loads(your_json)
5>>> print(json.dumps(parsed, indent=4, sort_keys=True))
6[
7 "foo",
8 {
9 "bar": [
10 "baz",
11 null,
12 1.0,
13 2
14 ]
15 }
16]
17
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"])
11
1#load and print elements of a json file
2import json
3file = "my_json_file.json"
4
5Json = json.load(open(file)) #Json is a dictionary
6
7print(Json)
8#OUTPUT: '{ "name":"John", "age":30, "city":"New York"}'
9
10print("Hello",Json["name"])
11#OUTPUT: Hello John