Normalize a nested json file in Python

Question:

Suppose a list of objects: one, two, three,…

Every object is composed of name, foo1, and foo2 fields.

[{
    'name':'one',
    'foo2':{ 
        'id':'1.1',
        'id':'1.2'
    },
    'foo1':[
        {
            'foo2':{
                'id':'1.1',
                'name':'one.one'
            }
        }, 
        {
            'foo2':{
                'id':'1.2',
                'name':'one.two'
            }
        }
    ]
}]

If the object is normalize using:

obj_row_normalize = pd.json_normalize( object_row,
                                  record_path =['foo2'],
                                   meta=['name'],
                                   record_prefix='num_',
               )

The result is:

id  num_name  
1.1      one   
1.2      one 

Given that, how can foo1.foo2.name be added to each resulting row as below:

id  num_name   name
1.1      one   one.one
1.2      one   one.two
Asked By: Diego Quirós

||

Answers:

Change the record path to foo1:

pd.json_normalize(recs, record_path=['foo1'], meta='name')
  foo2.id foo2.name name
0     1.1   one.one  one
1     1.2   one.two  one
Answered By: C.Nivs
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.