0

I'm running the latest Python3 (with the Anaconda distribution) and have a problem with the standard library installed json which causes the Traceback:

    Traceback (most recent call last):
      File "C:\Users\Think\Anaconda3\lib\site-packages\werkzeug\serving.py", line 193, in run_wsgi
        execute(self.server.app)
      File "C:\Users\Think\Anaconda3\lib\site-packages\werkzeug\serving.py", line 181, in execute
        application_iter = app(environ, start_response)
      File "C:\Users\Think\my_server.py", line 148, in __call__
        return self.wsgi_app(environ, start_response)
      File "C:\Users\Think\my_server.py", line 144, in wsgi_app
        response = self.dispatch_request(request)
      File "C:\Users\Think\my_server.py", line 80, in dispatch_request
        return getattr(self, 'on_' + endpoint)(request, **values)
      File "C:\Users\Think\my_server.py", line 54, in on_xapi_request
        json_data = self.load_json(request.data)
      File "C:\Users\Think\my_server.py", line 60, in load_json
        return json.loads(data)
      File "C:\Users\Think\Anaconda3\lib\json\__init__.py", line 312, in loads
        s.__class__.__name__))

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

But, simplejson doesn't cause the error.

2
  • You are only making statement. This is a Q&A site, it helps to put an interrogative sentence in your posts, so we don't have to guess what the question might be. Commented Sep 7, 2016 at 18:30
  • + you should give us some code, not only traceback :) Commented Sep 7, 2016 at 18:32

1 Answer 1

1

Use str.decode('utf-8') before passing to json.loads

I think this line:

return json.loads(data)

is causing the problem. Decode data before passing it to this function.

Sign up to request clarification or add additional context in comments.

2 Comments

Just using return json.loads(data.decode("utf-8")) works too and is more concise.
That did it. Many thanks. It is weird that simplejson worked and json didn't. I'm guessing it's because python3 is strict about str and bytes but simplejson isn't.

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.