-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshow_json_01.py
32 lines (26 loc) · 1.11 KB
/
show_json_01.py
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
# -*- coding: utf-8 -*-
import json
def breakdown(json_data, level=0): # A function to unfold json data # json_data - an object of json.load
position1 = ' ' * 3 * level
if type(json_data) != list and type(json_data) != dict:
print('\n', position1 + str(json_data) + ',', end='')
elif type(json_data) == list:
print('\n', position1, 'level ', level + 1, type(json_data))
print(position1 + '[', end='')
for x in json_data:
breakdown(x, level + 1)
print('\n', position1 + ']')
elif type(json_data) == dict:
print('\n', position1, 'level ', level + 1, type(json_data))
print(position1 + '{')
for x in json_data.items():
print(position1 + x[0] + ': ', end="")
if type(x[1]) != list and type(x[1]) != dict:
print(str(x[1]) + ',')
else:
breakdown(x[1], level + 1)
print(position1 + '}', end='')
filename = 'json_data_01.json'
with open(filename, encoding='UTF-8-sig') as file: # open a json file
data = json.load(file)
breakdown(data) # call function breakdown()