Created
September 26, 2013 01:49
-
-
Save higarmi/6708779 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 |
The code doesn't work for me.The output has many missing keys.
Input File = {
"flow": {
"hosts": {
"name": "Client",
"children": [
{
"name": "www.purizon.co.nz",
"children": [
{
"name": "www.purizon.co.nz"
}
]
},
{
"name": "img.oduvanchiksawa.biz",
"children": [
{
"name": "pon.dedulkasanya.biz"
}
]
},
{
"name": "fpdownload2.macromedia.com"
},
{
"name": "ie9cvlist.ie.microsoft.com"
}
]
},
"size": 180
}
}
Output: {u'flow.size': 180, u'flow.hosts.name': u'Client', u'flow.hosts.children.children.name': u'pon.dedulkasanya.biz', u'flow.hosts.children.name': u'ie9cvlist.ie.microsoft.com'}
Is inverse function available?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@guneysus Use the below code.