Skip to content
Advertisement

Filter null values from JSON

I receive the following JSON from the MS Graph API:

JavaScript

How can I filter the null values using Python (3.9) ?

Advertisement

Answer

First of all, processing it is easier when it’s been converted to python data structure, such as a dict. The built-in json module can convert a json-string into python objects using json.loads.

JavaScript

You can create a new dictionary without the None-values by iterating over the items. Python uses None in place of null, so we have to filter on that

JavaScript

Or more compactly:

JavaScript

Your example data is a list of dictionaries, so you’ll have to this to each element in the list.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement