How to remove null, false values from JSON file?

I have a JSON file with this data

{

    "in_reply_to_screen_name": null,
    "favorited": false,
    "id_str": "92",
    "entities": {
        "user_mentions": [],
        "symbols": [],
        "urls": [],
        "hashtags": [
            {
                "indices": [0,8]
            }
        ]
    },
    "geo": null,
    "user": {
        "verified": false,
        "notifications": null,
        "profile_sidebar_border_color": "FFFFFF",
        "geo_enabled": true,
        "profile_background_tile": true,
        "url": null,
        "id": 278,
        "default_profile": false,
        "lang": "pt",
        "location": null,
        "translator_type": "none",
        "protected": false
    },
    "id": 92,
    "in_reply_to_status_id_str": null,
    "in_reply_to_status_id": null,
    "created_at": "Tue Oct",
    "is_quote_status": false,
    "text": "This is a vdd",
    "truncated": false,
    "retweeted": false
}

How can I delete any key-value pairs that contain null, false and true of this file, using Python?

These values can appear in various level of the data structure.

Upvotes: 0

Views: 5331

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122382

By decoding, processing the object recursively, and encoding to JSON again.

I like to use single dispatch for such tasks:

from functools import singledispatch

@singledispatch
def remove_null_bool(ob):
    return ob

@remove_null_bool.register(list)
def _process_list(ob):
    return [remove_null_bool(v) for v in ob]

@remove_null_bool.register(dict)
def _process_list(ob):
    return {k: remove_null_bool(v) for k, v in ob.items()
            if v is not None and v is not True and v is not False}

data = json.load(source)
json.dump(dest, remove_null_bool(data))

I used is not False, etc. to test for the exact objects. Had I used v not in {None, False, True} the integer values 0 and 1 would be removed too as False and True are equal to those values, respectively.

Demo against your sample loaded into data:

>>> print(json.dumps(remove_null_bool(data), indent=4, sort_keys=True))
{
    "created_at": "Tue Oct",
    "entities": {
        "hashtags": [
            {
                "indices": [
                    0,
                    8
                ]
            }
        ],
        "symbols": [],
        "urls": [],
        "user_mentions": []
    },
    "id": 92,
    "id_str": "92",
    "text": "This is a vdd",
    "user": {
        "id": 278,
        "lang": "pt",
        "profile_sidebar_border_color": "FFFFFF",
        "translator_type": "none"
    }
}

Upvotes: 11

Related Questions