-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseJson.py
More file actions
46 lines (39 loc) · 861 Bytes
/
parseJson.py
File metadata and controls
46 lines (39 loc) · 861 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
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/python
import json
strJson = '\
{\
"id": 1,\
"name": "Foo",\
"price": 123,\
"tags": [\
"Bar",\
"Eek"\
],\
"stock": {\
"warehouse": 300,\
"retail": 20\
}\
}'
objJson=json.loads(strJson)
#print the json objects with keys and values
print objJson
print ('')
# print the values of each key
print objJson['stock']
print objJson['price']
print objJson['tags']
print objJson['id']
print objJson['name']
print ('')
# print each key in the string with its value
for item in objJson:
print ('Key: ' + item)
print ('Value: ' + str(objJson[item]))
print (type(objJson[item]))
if isinstance(objJson[item], dict) or isinstance(objJson[item], list):
for subItem in objJson[item]:
print ('Value: ' + subItem)
print ('')
print ('')
# pretty print Json string
print json.dumps(objJson, sort_keys=True, indent=4, separators=(',', ': '))