1

I have a project where I have to get JSON data from one API endpoint and then pull some data from this JSON data and send it to another API.

From first API I am getting this data:

response ={  
   "abc":"AP003",
   "data":[  
      {  
         "d":{  
            "e":"some data",
            "f":"some data"
         }
      }
   ]
}

I extract data from this and send it to another view.

response = response.json()
response = response['data']
return JsonResponse(json.dumps(response),safe=False)

Even below code does not work

response = response.json()
response = response['data']
return JsonResponse(response,safe=False)

When I do this I get response but data I get is in string form and not in JSON form. How to get JSON data and send it to template as it is?

Edit:

I have a JSON in a string format and I want it to send as JSON form in my API response

16
  • 1
    just pass response, no need to use json dumps Commented May 18, 2018 at 11:00
  • If I do that then I am getting '"/data/":/[ { / "/d/":/{ "e":"some data", "f":"some data" } } ]' Commented May 18, 2018 at 11:03
  • @BugHunter Getting / after every word. Commented May 18, 2018 at 11:04
  • You have to use data.json() . Commented May 18, 2018 at 11:04
  • @UsmanMaqbool It gives error. I have edited question to avoid confusion. Commented May 18, 2018 at 11:07

1 Answer 1

2

If you have response dict as follows, then there is no need to perform extra operations. you can directly pass that to JsonResponse, which will be converted to json.

response ={  
   "abc":"AP003",
   "data":[  
      {  
         "d":{  
            "e":"some data",
            "f":"some data"
         }
      }
   ]
}

return JsonResponse({"data": response['data']})
Sign up to request clarification or add additional context in comments.

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.