how to eliminate null values from json

Question:

I have to convert csv to json and eliminate null values.

Csv

ID   Col1   Col2  Col3  Col4
1    123    Null  ABC   Null
2    Null   456   ABC   Null
3    123    345   ABC   Null

I would like to eliminate all the null values and then create a json file or create json and eliminate all null values.

My json file should like :

{"ID":"1","Col1":"123","Col3":"ABC"}
{"ID":"2","Col2":"456","Col3":"ABC"}
{"ID":"2","Col1":"123","Col2":"345","Col3":"ABC"}

I tried with dropna(axis=0/1,how=all/any) in csv 

Also, after writing to json, I read the json file again to a dataframe and use below code

with open('data.json') as f:
    json_dict = json.load(f)
for key in json_dict:
    if json_dict[key] is NULL:
         json_dict.pop(key)

Both are not working.

Can anyone help me eliminate null values?

Asked By: unicorn

||

Answers:

You can built a dictionary key value from that json, then use this function below:

def remove_null_values(d: dict) -> dict:
    return {
        key: remove_null_values(value) if isinstance(value, dict) else value
        for key, value in d.items()
        if value != None
    }
Answered By: Ali Soussi

You can try export row by row

df = df.replace('Null', pd.NA)

with open('data.json', mode='a', newline='n') as f:
    for idx, row in df.iterrows():
        row.dropna().to_json(f)
        f.write('n')
data.json

{"ID":1,"Col1":"123","Col3":"ABC"}
{"ID":2,"Col2":"456","Col3":"ABC"}
{"ID":3,"Col1":"123","Col2":"345","Col3":"ABC"}
Answered By: Ynjxsjmh

Try this:

def remove_empty_elements(d):
"""recursively remove empty lists, empty dicts, or None elements from a dictionary"""

def empty(x):
    return x is None or x == {} or x == []

if not isinstance(d, (dict, list)):
    return d
elif isinstance(d, list):
    return [v for v in (remove_empty_elements(v) for v in d) if not empty(v)]
else:
    return {k: v for k, v in ((k, remove_empty_elements(v)) for k, v in d.items()) if not empty(v)}

Source: https://gist.github.com/nlohmann/c899442d8126917946580e7f84bf7ee7

Answered By: Avi B.

Try this piece of code. It should work:

import pandas as pd

df = pd.read_csv(name_of_the_file)

print(df.dropna(axis=1).to_dict("records"))

Here we are using pandas library to read csv file then we are using dropna method which is used to drop all the null from row/columns. Axis 1 represents we have to drop null columns

Answered By: Jyoti Thakkar
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.