1import json
2
3x = '{ "name":"John", "age":30, "city":"New York"}'
4y = json.loads(x)
5
6print(y["age"])
1import json
2
3json_file = json.load(open("your file.json", "r", encoding="utf-8"))
4
5# For see if you don't have error:
6print(json_file)
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# 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#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