0

I got some data from an API with requests:

r = requests.get(...)
a = r.text
print(type(a))
str2JSON = json.dumps(a,indent=4)
print(type(str2JSON))

The result is:

class 'str'
class 'str'

Then I try loads instead of dumps:

str2JSON_2 = json.loads(a)
print(type(str2JSON_2))

And I get class list!!!

Why is that behaviour?

If you dump a string into JSON and you don’t get an error, does it automatically mean that the JSON is well parsed? Shouldn't that be a JSON class?

3
  • Looks like your result wasn't included in the question Commented Oct 22, 2019 at 17:08
  • 2
    Did you mean to use loads instead of dumps? Commented Oct 22, 2019 at 17:11
  • 1
    The output is correct as the dumps() function (as in dump S) returns a string, as suggested by the S in dumps(). Commented Oct 22, 2019 at 17:12

3 Answers 3

4

The thing you get back from requests is a str value containing a JSON encoded value.

dumps takes that str and produces another string containing the JSON-encoded version of the original (JSON-encoded) string.

You need loads to decode the string into a value.

json2str = json.loads(a,indent=4)  # name change to reflect the direction of the operation

Consider:

>>> s = '"foo"'  # A JSON string value
>>> json.dumps(s)
'"\\"foo\\""'
>>> json.loads(s)
'foo'

The string may, of course, encode a value other than a simple string:

>>> json.loads('3')  # Compare to json.loads('"3"') returning '3'
3
>>> json.loads('[1,2,3]')
[1,2,3]
>>> json.loads('{"foo": 6}')
{'foo': 6}

requests, though, doesn't actually require you to remember which direction dumps and loads go (although you should make it a point to learn). A Response object has a json method which decodes the text attribute for you.

json2str = r.json()  # equivalent to json2str = json.loads(r.text)
Sign up to request clarification or add additional context in comments.

Comments

1

you are using requests. it provides convince method to parse your response as json (i.e. it loads it for you) you want a = r.json(). This way a would be JSON object and you can dump it as string later on. That is assuming you get valid json as response.

here is an example

import requests
import json

url = 'https://reqres.in/api/users' # dummy resposnse

resp =  requests.get(url)
my_json = resp.json()
#print example user
print(my_json['data'][0])
json_string = json.dumps(my_json, indent=4)
print(json_string)

7 Comments

What do you get there inserting : type(my_json) ?
try it yourself :-)
I did: HTTPSConnectionPool(host='reqres.in', port=443) Max retries exceeded etc. I can not connect to that API.
it's just a dummy api. if for whatever reason you cannot connect to it - try with your API url
< class 'list'>
|
0

json.dumps returns a JSON formatted python string object.

Below the is the statement you can notice in actual implementation file for def dumps

"""Serialize obj to a JSON formatted str.

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.