0

I've got json string, and I need to load it but it comes from TCP socket so it is in bytes. Normal decoding with utf-8 doesn't work, because in thah string there are float numbers in 4bytes format.

data = b'{"X1":"Cf\x19\x9a","X2":"\x00\x00\x00\x00"}'

how to load this into json.loads?

9
  • 3
    That's not valid json, so you can't parse it with json.load. Commented Sep 21, 2018 at 10:54
  • I know that this is not valid json, but how to decode it, and not lose any data, even zeros? Commented Sep 21, 2018 at 10:55
  • 1
    This is not valid JSON, because it does not contain data encoded with UTF-8, UTF-16 or UTF-32 codecs. Commented Sep 21, 2018 at 11:00
  • Ok, that's clear now Commented Sep 21, 2018 at 11:01
  • 1
    The \xhh byte points are just representations of the actual byte values; your data does not contain literal ` and x` bytes, each \xHH sequence is one byte. But the hex sequence 19 9A is not a valiid UTF-8 byte sequence, so this can't be parsed as JSON just for that reason. Commented Sep 21, 2018 at 11:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.