1

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 output:

  status  desc  actionable err_code err_msg
0   pass  desc       False     None    None
1   fail  desc        True     None    None
John Stud
  • 1,506
  • 23
  • 46

2 Answers2

2

If your json objects are under the xd columns, you can exctract that json, which is a list of dictionaries. A list of dictionaries can 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?

1

You could explode + tolist + DataFrame constructor:

out = pd.DataFrame(df['xd'].explode().tolist())

Output:

  status  desc  actionable err_code err_msg
0   pass  desc       False     None    None
1   fail  desc        True     None    None