Newer
Older
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
def flatten_json(y):
out = dict()
def flatten(x, name=''):
if type(x) is dict:
for a in x:
flatten(x[a], name + a + '.')
elif type(x) is list:
i = 0
for a in x:
flatten(a, name + str(i) + '.')
i += 1
else:
out[name[:-1]] = x
flatten( y )
return out
def is_cardinal( x ):
try:
i = int(x)
return True
except:
return False
def cardinal_key_to_list(x):
if type(x) is not dict:
return x
elif type(x) is dict and all( [is_cardinal(y) for y in x.keys() ] ):
return [cardinal_key_to_list(v) for v in x.values()]
elif type(x) is dict:
return {k: cardinal_key_to_list(v) for k, v in x.items()}
def unflatten_json(y):
output = dict()
for key, value in y.items():
parts = key.split(".")
d = output
for j, part in enumerate(parts[:-1]): # parts[:-1] = all terms but last
if part not in d:
d[part] = dict()
d = d[part]
d[parts[-1]] = value
return cardinal_key_to_list(output)
if __name__ == '__main__':
from pprint import pprint
import json
in1 = {"responsibleParty": [
{
"email": "data@grandlyon.com",
"role": "custodian",
"address": "20 rue du Lac, CS 33569, Lyon cedex 03, 69505, France",
"individualName": "Géomatique et données métropolitaines",
"logo": "https://download.data.grandlyon.com/catalogue/images/harvesting/GrandLyon.gif",
"appliesTo": ["resource1", "resource2"],
"organisationName": "Metropole de Lyon / Direction Innovation Numérique et Systèmes d'Information (DINSI)"
},
{
"email": "data@grandlyon.com",
"role": "custodian",
"address": "une autre adresse",
"individualName": "Géomatique et données métropolitaines",
"logo": "https://download.data.grandlyon.com/catalogue/images/harvesting/GrandLyon.gif",
"appliesTo": "metadata",
"organisationName": "Metropole de Lyon / Direction Innovation Numérique et Systèmes d'Information (DINSI)"
}
]}
#in1 = dict(in1)
flattened_json = flatten_json(in1)
unflattened_json = unflatten_json(flattened_json)
print('ORIGINAL')
pprint(in1)
print('')
print('FLATTENED')
pprint(flattened_json)
print('')
print('UNFLATTENED')
pprint(unflattened_json)
assert json.dumps(in1, sort_keys=True) == json.dumps(unflatten_json(flatten_json(in1)), sort_keys=True)
exit(0)
print(' ')
with open('flatten_original.json', 'w') as fp:
json.dump(in1, fp, sort_keys=True)
with open('flatten_processed.json', 'w') as fp:
json.dump(unflatten_json(flatten_json(in1)), fp, sort_keys=True)
print(' ')
pprint(json.dumps(unflatten_json(flatten_json(in1)), sort_keys=True))