Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing \u0000 from nested JSON object before INSERTing into PostgreSQL

I am using Python to insert JSON objects into a PostgreSQL DB table. The JSON nested are highly nested. The strings in some of these JSON objects include '\u0000', which is an illegal character and UNICODE and must be sanitized before inserting into PG.

What would be the fastest way of doing that?

like image 910
Sleepster Avatar asked Aug 03 '26 14:08

Sleepster


2 Answers

Can you please see if this works for you as a parameter in your call to execute()?

>>> a = '\u0000'
>>> json.dumps(a).replace(r'\u0000', '')
'""'
>>> 

The r'' is for a raw string to help get around quoting problems.

like image 58
Mike Organek Avatar answered Aug 05 '26 08:08

Mike Organek


Here is what I've come up with.

def sanitize(obj):
    if isinstance(obj, str):
      return obj.replace('\u0000', '')
    if isinstance(obj, list):
      return [sanitize(item) for item in obj]
    if isinstance(obj, tuple):
      return tuple([sanitize(item) for item in obj])
    if isinstance(obj, dict):
      return {k:sanitize(v) for k,v in obj.items()}
    return obj

Is there a more elegant solution that I'm missing here?

like image 32
Sleepster Avatar answered Aug 05 '26 09:08

Sleepster