0

Python newbie here, I am working on a python app which is working fine in version 2.7 and is using flask version 0.11. After the code is updated to the latest python version it seems jsonify from flask is returning bytes instead of a string.

For example jsonify({'name': 'Test'}) results in b'{"name": "Test"}\n'. I want to get the JSON string. Am I missing something?

Hector Barbossa
  • 5,506
  • 13
  • 48
  • 70
  • 1
    Does this answer your question? [json.dumps vs flask.jsonify](https://stackoverflow.com/questions/7907596/json-dumps-vs-flask-jsonify) – Olvin Roght Jul 20 '20 at 20:02
  • @OlvinRoght Thanks but no, in this case jsonify is returning bytes and I am trying to understand why. As this is an old working code, I am trying to understand why its breaking after the version upgrade. – Hector Barbossa Jul 20 '20 at 20:07
  • It doesn't return bytes, it returns `Response` object, at least have to do that in latest version. *(this information also mentioned in selected answer of question I've pointed you to)* – Olvin Roght Jul 20 '20 at 20:08
  • @OlvinRoght Yes, correct. But in version 2.7 the same code returns ```{"name": "Test"}``` – Hector Barbossa Jul 20 '20 at 20:09
  • There's no `bytes` in python2 so I guess that somewhere deep developers of flask decided to use string for compatibility. Actually in python2 string is the only binary sequence type. – Olvin Roght Jul 20 '20 at 20:18
  • 2
    Could you please post the code snippet to better understand the problem..Thank you..!! – Shraddha Jul 20 '20 at 20:20

0 Answers0