I use Python 3 . Json comes to me in this format:

 b'{\n "begindate": "2016-11-22", \n "enddate": "2016-11-22", \n "guids": ["6593062E-9030-B2BC-E63A-25FBB4723ECC", \n "5A9F8478-6673-428A-8E90-3AC4CD764543", \n "D8243BA1-0847-48BE-9619-336CB3B3C70C"]\n}' 

When trying to do json.loads() an error occurs:

 TypeError: the JSON object must be str, not 'bytes' 

If you make str() , the string will turn into this:

 'b\'{\\n "begindate": "2016-11-22", \\n "enddate": "2016-11-22", \\n "guids": ["6593062E-9030-B2BC-E63A-25FBB4723ECC", \\n "5A9F8478-6673-428A-8E90-3AC4CD764543", \\n "D8243BA1-0847-48BE-9619-336CB3B3C70C"]\\n}\'' 

Parse it even harder. What to do ?

  • Non-English characters in zhsone just will not? - andreymal

1 answer 1

Since json came to you as a byte string, you first need to translate the received message into a string using the decode method, so you will get a string. Next, convert this line to json using the loads method.

Like this:

 s = b'{"key": "value"}' # ваша строка json.loads(s.decode())