My advice - above- with string replacement would have worked too, but this is a proper way.
You cannot change nested structures without diving in with recursion
This simple code produces the same result (but nobody listens )
You cannot change nested structures without diving in with recursion
def replace_nulls(json_elem): if isinstance(json_elem, list): return [replace_nulls(elem) for elem in json_elem] elif isinstance(json_elem, dict): return {key: replace_nulls(value) for key, value in json_elem.items()} else: return 'vacio' if json_elem is None else json_elem
Output:[{'contributors': 'vacio',
'coordinates': 'vacio',
'created_at': 'Sun Jun 10 14:21:53 +0000 2018',
'entities': {'hashtags': [],
'symbols': [],
'urls': [],
'user_mentions': [{'id': 254599256,
'id_str': '254599256',
'indices': [3, 11],
'name': 'Alejandro Fargosi',
'screen_name': 'fargosi'}]},
'favorite_count': 0,
'favorited': False,
'filter_level': 'low',
'geo': 'vacio',
'id': 1005817329459097600,
'id_str': '1005817329459097600',
'in_reply_to_screen_name': 'vacio',
'in_reply_to_status_id': 'vacio'}]
This simple code produces the same result (but nobody listens )
json.loads(json.dumps(data).replace('null', '"vacio"'))
Test everything in a Python shell (iPython, Azure Notebook, etc.)
- Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.