0

There are couple of questions related to this error:

JSON object must be str, not 'bytes'

However, except obvious solution to read and decode response, I didn't learn anything special.

Here is example to the problem:

>>> import json
>>> from urllib.request import urlopen
>>> url = 'http://echo.jsontest.com/key/value/one/two'
>>> with urlopen(url) as request:
...     json.load(request)
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "C:\Python35\lib\json\__init__.py", line 268, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "C:\Python35\lib\json\__init__.py", line 312, in loads
    s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'

So my question is why Python's JSON deserializer (that accepts file-like objects with .read() method), does not try to handle this request, as response headers hint all there is needed to know:

Content-Type: application/json; charset=ISO-8859-1

Headers hint, they do not guarantee, but that can not be a reason not to try the obvious IMHO.

vedar
  • 483
  • 8
  • 15
  • Because **you**, as the author, are expected to be explicit about what you want to happen. Python won't just quietly do something that could give completely unexpected results. What if there isn't a `charset` in the headers? What if there is one, but it's wrong? I'd recommend using `requests`, which *does* abstract a lot of this out. – jonrsharpe Jul 24 '16 at 11:21
  • Well, I can only do the same - check headers and decode and expect that all is fine. I'm just thinking why doesn't JSON deserializer do the same obvious thing. The error I'll potentially get will be just the same, as if I decode it "manually". – vedar Jul 24 '16 at 11:27
  • Thanks for `requests` suggestion, I needed only standard library for this. – vedar Jul 24 '16 at 11:28
  • Have a look here: http://stackoverflow.com/questions/4981977/how-to-handle-response-encoding-from-urllib-request-urlopen – Dean Fenster Jul 24 '16 at 11:49

1 Answers1

0

Use json.loads instead of json.load

sayan
  • 1,520
  • 17
  • 33