1

I am putting a JSON response into a variable via requests.json() like this:

response = requests.get(some_url, params=some_params).json()

This however converts JSON's original " to Python's ', true to True, null to None.

This poses a problem when trying to save the response as text and the convert it back to JSON - sure, I can use .replace() for all conversions mentioned above, but even once I do that, I get other funny json decoder errors.

Is there any way in Python to get JSON response and keep original JavaScript format?

2 Answers 2

1

json() is the JSON decoder method. You are looking at a Python object, that is why it looks like Python.

Other formats are listed on the same page, starting from Response Content

  • .text: text - it has no separate link/paragraph, it is right under "Response Content"
  • .content: binary, as bytes
  • .json(): decoded JSON, as Python object
  • .raw: streamed bytes (so you can get parts of content as it comes)

You need .text for getting text, including JSON data.

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

1 Comment

Another option would be serializing the object again. That would ensure it is valid and they all have the samef formatting. That means they wouldn't be saved verbatim though. Depends on use case.
1

You can get the raw text of your response with requests.get(some_url, params=some_params).text

It is the json method which converts to a Python friendly format.

Comments

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.