How to normalize complex nested json in python? -
i trying normalize complex nested json in python unable parse objects out.
i referencing code page. https://medium.com/@amirziai/flattening-json-objects-in-python-f5343c794b10
sample_object = {'name':'john', 'location':{'city':'los angeles','state':'ca'}, 'hobbies':['music', 'running']} def flatten_json(y): out = {} def flatten(x, name=''): if type(x) dict: in x: flatten(x[a], name + + '_') elif type(x) list: in x: flatten(a, name) else: out[name[:-1]] = x flatten(y) return out flat = flatten_json(sample_object) print json_normalize(flat)
return result:
name | location_city | location_state | hobbies -----+---------------+----------------+-------- john | los angeles | ca | running
expected result:
name | location_city | location_state | hobbies -----+---------------+----------------+-------- john | los angeles | ca | running john | los angeles | ca | music
the problem having originates in following section
elif type(x) list: in x: flatten(a, name)
because not change name every element of list, every next element override assignment of previous element , last element show in output.
applied example, when flattening function reaches list 'hobbies' first assign name 'hobbies' element 'music' , send output. after element 'music', next element in list 'running', asigned name 'hobbies'. when element send output notice name 'hobbies' exists , override value 'music' value 'running'.
to prevent script link referenced uses following piece of code append de array's index name, creating unique name every element of array.
elif type(x) list: = 0 in x: flatten(a, name + str(i) + ' ') += 1
this create 'columns' data rather new row. if latter want have change way functions set up. 1 way adapt function return list of json's (one each list element in original json).
an note: recommend beeing bit more carefull coppying code when submitting question. indenting bit of in case , since left out part import json_normalize might not clear importing pandas
from pandas.io.json import json_normalize
Comments
Post a Comment