Python Forum
Replace null values in Json file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replace null values in Json file
#7
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

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 Doh )
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.
Reply


Messages In This Thread
Replace null values in Json file - by burnsssss - Jun-10-2018, 05:08 PM
RE: Replace null values in Json file - by Grok_It - Jun-10-2018, 06:29 PM
RE: Replace null values in Json file - by buran - Jun-10-2018, 06:50 PM
RE: Replace null values in Json file - by burnsssss - Jun-11-2018, 07:50 AM
RE: Replace null values in Json file - by volcano63 - Jun-10-2018, 07:26 PM
RE: Replace null values in Json file - by buran - Jun-11-2018, 07:57 AM
RE: Replace null values in Json file - by volcano63 - Jun-11-2018, 08:18 AM
RE: Replace null values in Json file - by buran - Jun-11-2018, 08:44 AM
RE: Replace null values in Json file - by volcano63 - Jun-11-2018, 08:56 AM
RE: Replace null values in Json file - by burnsssss - Jun-11-2018, 08:52 AM
RE: Replace null values in Json file - by buran - Jun-11-2018, 08:58 AM
RE: Replace null values in Json file - by burnsssss - Jun-11-2018, 09:45 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Replace values in Yaml file with value in dictionary PelleH 0 156 Jun-12-2024, 02:40 PM
Last Post: PelleH
  encrypt data in json file help jacksfrustration 1 487 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  parse json field from csv file lebossejames 4 931 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Replace a text/word in docx file using Python Devan 4 4,468 Oct-17-2023, 06:03 PM
Last Post: Devan
  Need to replace a string with a file (HTML file) tester_V 1 924 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Python Script to convert Json to CSV file chvsnarayana 8 2,817 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,450 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  How to express null value klatlap 3 1,002 Mar-25-2023, 10:40 AM
Last Post: klatlap
  Converting a json file to a dataframe with rows and columns eyavuz21 13 5,326 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
Photo How to select NULL and blank values from MySQL table into csv python300 9 2,762 Dec-27-2022, 09:43 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020