3

I'd like to replace null values with empty array when using pandas json_normalize. here is the sample:

{
    "id": {
        "0": "x0123455",
        "1": "x0123456"
    },
    "team": {
        "0": null,
        "1": [
            {
                "name": "Jenny",
                "email": "jenny@gmail.com"
            }
        ]
    },

I read somewhere that I need to replace null values with empty dicts to avoid getting errors. How can I achieve this?

Edit

As a workaround, I filtered out null values

JamesBowery
  • 71
  • 10
  • Have you tried calling json_normalize on that dict to see what happens with the null values? – mquasar Nov 29 '21 at 01:22
  • it returns blank value when calling json_normalize. By the way, I'm working on response json and it's returning some null values, but other attributes have `[]` values. – JamesBowery Nov 29 '21 at 03:39

2 Answers2

1

I think you may do something simple, like

for item in items:
    for email in item['team'].values():
        if email == null:
            email = {}
0

When I ran JSON normalize function on this dictionary (although it was not a JSON object), it provided an output that included the None value.

import pandas as pd

test_dict ={
    "id": {"0": "x0123455", "1": "x0123456"},
    "team": {"0": None, "1": [{"name": "Jenny", "email": "jenny@gmail.com"}]},
}

df = pd.io.json.json_normalize(test_dict)

print(df)

This code produces the following output when I did df.to_csv:

,id.0,id.1,team.0,team.1 0,x0123455,x0123456,,"[{'name': 'Jenny', 'email': 'jenny@gmail.com'}]"

*Note this does not work if the value is null because there is no such thing as null in Python.

I suggest just running with json normalize if it works for you, if not this question on stack overflow should answer your question with recursive code to remove all null values.

Jawad
  • 11
  • 1