Home > Back-end >  How to find empty fields in json file?
How to find empty fields in json file?

Time:01-02

I have this sample.json file with me:

{   
    "details":[
    {
    "name": "",
    "class": "4",
    "marks": "72.6"
    },
    {
    "name": "David",
    "class": "",
    "marks": "78.2"
    },
    {
    "name": "Emily",
    "class": "4",
    "marks": ""
    }
    
    
]
}

As you can see for the first one; "name" is string datatype is actually empty. For the second one; "class" with integer datatype is empty. And for the third one; "marks" with float datatype is empty.

Now my task is; to find the fields which are empty, if string is empty replace it with "BLANK", if integer is empty replace it with 0, and if float is empty replace it with 0.0

P.S: I'm doing this with Python like this:

import json
path = open('D:\github repo\python\sample.json')
df = json.load(path)
for i in df["details"]:
    print(i["name"])

Can anybody help me with it! Thank You so much!

CodePudding user response:

Since the type info isn't available anywhere programmatically, and there seem to be only three hard-coded fields, I'd just check each of them explicitly.

Short-circuiting with the or operator would even allow you to achieve this fairly elegantly:

for d in df['details']:
    d['name'] = d['name'] or 'BLANK'
    d['class'] = d['class'] or '0'
    d['marks'] = d['marks'] or '0.0'

CodePudding user response:

You could check whether the string is empty with a simple if statement like so.

if not i['name'] == ""

Alternatively, you could also do

if not i['name']

The second if statement makes use of falsy and truthy values in Python. Here's a link to read more about it

CodePudding user response:

You could create a dictionary empty_replacements mapping each key to its corresponding desired empty value:

import json

sample_json = {
    "details": [
        {
            "name": "",
            "class": "4",
            "marks": "72.6"
        },
        {
            "name": "David",
            "class": "",
            "marks": "78.2"
        },
        {
            "name": "Emily",
            "class": "4",
            "marks": ""
        }
    ]
}

empty_replacements = {"name": "BLANK", "class": "0", "marks": "0.0"}

sample_json["details"] = [{
    k: v if v else empty_replacements[k]
    for k, v in d.items()
} for d in sample_json["details"]]

print('sample_json after replacements: ')
print(json.dumps(
    sample_json,
    sort_keys=False,
    indent=4,
))

Output:

sample_json after replacements:
{
    "details": [
        {
            "name": "BLANK",
            "class": "4",
            "marks": "72.6"
        },
        {
            "name": "David",
            "class": "0",
            "marks": "78.2"
        },
        {
            "name": "Emily",
            "class": "4",
            "marks": "0.0"
        }
    ]
}

CodePudding user response:

I 'm assuming by the dictionary which you provided that marks & class are stored as String.

li=[]
for d in df["details"]:
    for k,v in d.items():
        if (v==''):
            if (k=='name'):
                d[k]="BLANK"
            elif (k=='class') :
                d[k]='0'
            elif (k=='marks'):
                d[k]='0.0'
    li.append(d)
df['details']=li
  • Related