2

I have some external service that sends me next json:

json_str = b'{"a": "\xc3-----\xa4-----\x13"}'

When I trying to parse it, I get next error:

----> 1 json.loads(json_str)
ValueError: Invalid control character at: line 1 column 20 (char 19)

I managed to parse it correctly using next command:

In [37]: eval(json_str)
Out[37]: {'a': '\xc3-----\xa4-----\x13'}

Are there any ideas on how to parse it in another way?

Alex Tonkonozhenko
  • 1,514
  • 12
  • 26
  • 1
    JSON is, by definition, Unicode text. If you have a byte string, you need to first decode it to get the Unicode string that you can parse as JSON. Python is very strict about the difference between bytes and strings, even when the mapping between the two is trivial. – chepner Jan 23 '19 at 13:58
  • 1
    `eval` only "works" because the bytes happen to be a valid Python expression in addition to an encoding of a valid JSON expression. – chepner Jan 23 '19 at 13:59
  • Yes, now I see how to deal with it. Before this, I parsed it correctly without decoding and these symbols confused me. – Alex Tonkonozhenko Jan 23 '19 at 14:05
  • Note that the error refers to the single control character represented by `\x13`, which cannot be used directly in a JSON value. – chepner Jan 23 '19 at 14:08

1 Answers1

2

I found a way using .decode and the json.loads functions.

Hope this helps.

>>> json.loads(json_str.decode("latin-1"), strict=False)
{u'a': u'\xc3-----\xa4-----\x13'}

The output is still in unicode

References

json.loads error

decode error

Albin Paul
  • 3,330
  • 2
  • 14
  • 30