Skip to content Skip to sidebar Skip to footer

Normalizing Json List As Values

I have a problem to normalize a json, I can't find a solution in all the documentation I found. my json: {'id': 790005, 'company_id': 700025, 'owner_id': {'id': 11300075, 'name

Solution 1:

Try:

from pandas.io.json import json_normalize
df = json_normalize(data)
df['email'] = df['email'].apply(pd.Series) # to convert [{}]  --> {} df = pd.concat([df, df['email'].apply(pd.Series).add_prefix("email_")], axis=1)
df['phone'] = df['phone'].apply(pd.Series) # to convert [{}]  --> {} df = pd.concat([df, df['phone'].apply(pd.Series).add_prefix("phone_")], axis=1)
df.drop(['email', 'phone'], inplace=True, axis=1)

Then access by email_label, email_value ....

Solution 2:

Not the best answer possible, but very understandable code imho

result = {}
for key,value in x.items():
    iftype(x[key]) isnotlist:
        result[key] = x[key]
     eliftype(x[key]) islist:
         for i in x[key]:
            iftype(x[key]) isdict:
                 result[key] = x[key]['value']
# Simply using the input and iterating through to get all the relevant information # into a new result dictionary# now we read the info into a df

df = pd.DataFrame.from_dict(result,orient='index')

# You can play around with how you would like to keep the columns, Transpose if you like.

Post a Comment for "Normalizing Json List As Values"