-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSON.py
More file actions
37 lines (34 loc) · 785 Bytes
/
JSON.py
File metadata and controls
37 lines (34 loc) · 785 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# json module (JavaScript Object Notation)
# parsing JSON string by using json.loads() method - Python dictionary or list
import json
# dictionary (nested)
data = '''{
"name" : "Chuck",
"phone" : {
"type" : "intl",
"number" : "+1 734 303 4456"
},
"email" : {
"hide" : "yes"
}
}'''
info = json.loads(data) # dictionary
print('Name:',info['name'])
print('Hide:',info["email"]["hide"])
# dictionary as elements in LIST
input = '''[
{"id" : "001",
"x" : "2",
"name" : "Chuck"
},
{"id" : "009",
"x" : "7",
"name" : "Brent"
}
]'''
info = json.loads(input) # list
print('User count:',len(info))
for item in info:
print('Name:',item["name"])
print('Id:',item["id"])
print('Attribute:',item["x"])