skip to Main Content

I have a json file test.json,

{
    "person": [
        {
            "requestId": "0",
            "name": "ABC",
        "age" : 30,
            "city": ""
        }
    ]
}

Since city is null I dont want that in the json. so it should be like

{
    "person": [
        {
            "requestId": "0",
            "name": "ABC",
        "age" : 30,
            
        }
    ]
}

Also i wanted to update the same test.json file after removing null keys.

This is what I did and it did not remove the record.

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

Do I need to check value of the key separately?

2

Answers


  1. I think there are a few problems in your initial approach:

    • You’re only iterating over the keys in the outermost dictionary. This outermost dictionary has one key: "person", which has a non-empty value. So that key is not being removed.
    • You’re trying to change the size of a dictionary while you loop over it. This is an error in Python.
    • You want to remove the key "city" which has a value of "", but you’re comparing "" to None.
    • You’re not writing the modified JSON object back to your file.

    Here’s some code that addresses these problems:

    import json
    
    def recursively_remove_empty_keys(json_object):
        if isinstance(json_object, dict):
            keys_to_remove = []
            for key, value in json_object.items():
                if value == '':
                    keys_to_remove.append(key)
                recursively_remove_empty_keys(value)
            for key in keys_to_remove:
                del json_object[key]
        elif isinstance(json_object, list):
            for elem in json_object:
                recursively_remove_empty_keys(elem)
    
    def main():    
        filename = 'test.json'
        with open(filename) as f:
            json_object = json.load(f)
    
        recursively_remove_empty_keys(json_object)
    
        with open(filename, 'w') as f:
            json.dump(json_object, f, indent=4)
    
    main()
    

    This code loads a JSON object, traverses the whole object, and deletes any dictionary keys whose values are the empty string.

    Login or Signup to reply.
  2. As pointed out in the previous answer, there are 3 issues:

    1. nested dict in a list
    2. changing collections (lists or dicts) whilst looping is problematic.
    3. "" is none in json, but is it an empty string (string len = 0) in python.

    If you know the structure, then you can do this:

    d = {
        "person": [
            {
                "requestId": "0",
                "name": "ABC",
                "age" : 30,
                "city": ""
            }
        ]
    }
    
    # the bit you want to clean
    json_dict:dict = d['person'][0]
    
    # clean the inner dict
    clean_dict = {}
    for key in json_dict:
        if json_dict[key] != '':
            clean_dict[key] = json_dict[key]
    
    d = {
        "person": [
            clean_dict
        ]
    }
    
    print(d)
    

    which will return this:

    {'person': 
        [
            {  
                'requestId': '0', 
                'name': 'ABC', 
                'age': 30
            }
        ]
    }
    

    Alternatively, for a more general solution, you should use a recursive method that traverses the structure of the object.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search