Forked from higarmi/Flatten JSON or a nested dictionary
Created
September 28, 2017 10:03
-
-
Save CyberLight/1a238a0c65523e6b70fe9ecf773a70a8 to your computer and use it in GitHub Desktop.
This python recursive function flattens a JSON file or a dictionary with nested lists and/or dictionaries. The output is a flattened dictionary that use
dot-chained names for keys, based on the dictionary structure. This allows for reconstructing the JSON structure or converting it to
other formats without loosing any structural information.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
example: The following JSON document: | |
{"maps":[{"id1":"blabla","iscategorical1":"0", "perro":[{"dog1": "1", "dog2": "2"}]},{"id2":"blabla","iscategorical2":"0"}], | |
"masks":{"id":"valore"}, | |
"om_points":"value", | |
"parameters":{"id":"valore"}} | |
will have the following output: | |
{'masks.id': 'valore', 'maps.iscategorical2': '0', 'om_points': 'value', 'maps.iscategorical1': '0', | |
'maps.id1': 'blabla', 'parameters.id': 'valore', 'maps.perro.dog2': '2', 'maps.perro.dog1': '1', 'maps.id2': 'blabla'} | |
""" | |
def flattenDict(d, result=None): | |
if result is None: | |
result = {} | |
for key in d: | |
value = d[key] | |
if isinstance(value, dict): | |
value1 = {} | |
for keyIn in value: | |
value1[".".join([key,keyIn])]=value[keyIn] | |
flattenDict(value1, result) | |
elif isinstance(value, (list, tuple)): | |
for indexB, element in enumerate(value): | |
if isinstance(element, dict): | |
value1 = {} | |
index = 0 | |
for keyIn in element: | |
newkey = ".".join([key,keyIn]) | |
value1[".".join([key,keyIn])]=value[indexB][keyIn] | |
index += 1 | |
for keyA in value1: | |
flattenDict(value1, result) | |
else: | |
result[key]=value | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment