0

I'm using psycopg2 to save a JSON file to a PSQL database. I keep getting this error: ValueError: A string literal cannot contain NUL (0x00) characters.

How do you detect and remove a NUL character from a string? Can't find an answer that's not in C.

Also, this is NOT a standard find/replace character question. So far as I can tell, a NUL character is not a whitespace or newline - I don't know how it's encoded in UTF-8.

Error message:

    sudo python3 json_to_psql.py -f /home/data/aggressive_dedup.json -n reviews -s reviews_schema.json
/home/.local/lib/python3.6/site-packages/psycopg2/__init__.py:144: 
Traceback (most recent call last):
  File "json_to_psql.py", line 89, in <module>
    insert_json_into_psql(args.name, columns, elements, cur)
  File "json_to_psql.py", line 57, in insert_json_into_psql
    execute_values(cursor, insert_query, values)
  File "/home/rob/.local/lib/python3.6/site-packages/psycopg2/extras.py", line 1271, in execute_values
    parts.append(cur.mogrify(template, args))
ValueError: A string literal cannot contain NUL (0x00) characters.
  • 1
    Where are you getting the string? A valid JSON file can't contain a null byte. You'll need to show some code that triggers your error. – chepner May 26 '18 at 23:19
  • Can you post the entire error message so we can see the failing line? – tdelaney May 26 '18 at 23:57
  • 1
    Perhaps its `somestring.replace("\x00", "")` or trim it with `somestring[:somestring.find('\x00')]`, but then you've just pushed a corrupt string into the db which is likely worse. – tdelaney May 27 '18 at 00:00

0 Answers0