Michael Anaman
Michael Anaman

Reputation: 199

'str' object has no attribute 'get' - Python

I am trying to get elements from my json response , however i am getting an error

'str' object has no attribute 'get'.

What could be wrong in my code please help

        dataform = json.loads(job.body)
        data = json.dumps(dataform)

        logger.debug(data)
        logger.debug(data.get('number').strip())

My Reponse is below

{"shop_id": "23823addsf33sdfdlladioiddf", "user_id": "1", "number": "440239023011"}

Upvotes: 0

Views: 501

Answers (2)

Elijah
Elijah

Reputation: 133

Cause you get str after json.dumps().

In [2]: json.dumps({"a": 1})
Out[2]: '{"a": 1}'

dataform.get('number') will work

Upvotes: 0

blue note
blue note

Reputation: 29081

json.dumps turns you dictionary into a string. After applying it, you have something like

data = "\{\"shop_id\": \"23823addsf33sdfdlladioiddf\" ..."

So, it's not a dictionary anymore, it's a string that looks like a dictionary. That's why you get the error. You should probably apply get to the previous line.

Upvotes: 2

Related Questions