How to remove null, false and true values from Json file?

Asked

Viewed 393 times

1

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": "Esta \u00e9 a vdd",
    "truncated": false,
    "retweeted": false
}

How can I delete the key/value pair that contains value null, false and true of this file using Python?

Knowing that these values that should be deleted appear in various parts of the dictionary and when I try to do:

with open(arq_geral,'r') as f:
for line in f:
    tweet = json.loads(line)
    dados_tweet.append(tweet)


for tweet in dados_tweet:
    if tweet["geo"]:
        del tweet["geo"]

It does not remove geo, for example.

1 answer

2


Just do a recursive function:

def filter_dict_by_value(dicionary, values):
    result = {}
    for key, value in dicionary.items():
        if type(value) is dict:
            result[key] = filter_dict_by_value(value, values)
        elif value not in values:
            result[key] = value
    return result

Scroll through all key/value pairs in the dictionary; if the value is another dictionary, call the function again for this value; otherwise, if the value is not in the list values will be kept in the final dictionary; if the value is in the list, it will be ignored.

The code would look something like:

import json

with open("data.json") as json_file:
    data = json.load(json_file)

def filter_dict_by_value(dicionary, values):
    result = {}
    for key, value in dicionary.items():
        if type(value) is dict:
            result[key] = filter_dict_by_value(value, values)
        elif value not in values:
            result[key] = value
    return result

print(filter_dict_by_value(data, [None, False, True]))

See working on Repl.it

Producing the output:

{
    'id_str': '92', 
    'entities': {
        'user_mentions': [], 
        'symbols': [], 
        'urls': [], 
        'hashtags': [
            {
                'indices': [0, 8]
            }
        ]
     }, 
     'user': {
         'profile_sidebar_border_color': 'FFFFFF', 
         'id': 278, 
         'lang': 'pt', 
         'translator_type': 'none'
     }, 
     'id': 92, 
     'created_at': 'Tue Oct', 
     'text': 'Esta é a vdd'
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.