Source code for parsons.utilities.json_format
[docs]
def remove_empty_keys(dirty_dict):
"""
Remove empty keys from a dictionary. This method is useful when passing jsons
in which a null field will update the value to null and you don't want that.
"""
clean_dict = {}
for k, v in dirty_dict.items():
if v:
clean_dict[k] = v
return clean_dict
[docs]
def flatten_json(json):
"""
Flatten nested json to return a dict without nested values. Lists without nested values will
be ignored, and lists of dicts will only return the first key value pair for each key. Useful
for passing nested json to validation methods.
"""
out = {}
def flatten(x, name=""):
if type(x) is dict:
for k, v in x.items():
flatten(v, k)
elif type(x) is list:
for a in x:
flatten(a)
elif name != "" and name not in out:
out[name] = x
flatten(json)
return out