Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas: JSON Normalize with brackets around the JSON?

How might I use json_normalize to breakdown a nested JSON object that is contained in a list?

Consider this example:

df = pd.DataFrame({'xd': [
    [{
        "status": "pass",
        "desc": "desc",
        "actionable": False,
        "err_code": "None",
        "err_msg": "None"
    }],
    [{
        "status": "fail",
        "desc": "desc",
        "actionable": True,
        "err_code": "None",
        "err_msg": "None"
    }] ]})


pd.json_normalize(df['xd'])  # not expected



expected = pd.DataFrame({'status': ['pass'],
                         'desc': ['desc'],
                         'actionable': False})
                        # etc..
# expected out
    status  desc    actionable
0   pass    desc    False

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If your json objects are under the xd columns, you can exctract that json, which is a list of dictionaries. A list of dictionaries cand be used to create a dataframe object, from here.

list_of_dicts = list_of_dicts=list(map(lambda l: l[0], df['xd'].to_list()))
expected = pd.Dataframe(list_of_dicts)

Does this answer your question?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading