Removing \u0000 from nested JSON object before INSERTing into PostgreSQL

Viewed 1831

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?

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.

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?

Related