0

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"}
1
  • 1
    data = json.dumps(dataform) creates a string. Commented Sep 24, 2019 at 10:39

2 Answers 2

2

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.

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

3 Comments

how do i get the element from data then and not dataform ? i want to access the json element
@MichaelAnaman: impossible to know, depends on the structure of you object. It could be by applying get to the job.body directly, but just guessing
"it's a string that looks like a dictionary. " => actually, to be exact, it's a string representing the JSON version of the dict.
0

Cause you get str after json.dumps().

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

dataform.get('number') will work

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.