-4

I have an API which gives me the following data in response.text, How can I caonvert this data into a python dictionary?

response.text

[{"_id":"5dccedadff47e867a2833819","tel":"XXXX","loc":[28.498692,77.095215],"tripId":"5dccedaaff47e867a28337ec","mode":"automated","osm_data":{"distance_remained":10791,"time_remained":1649.5},"distance_remained":10870,"time_remained":1173,"curr_ETA":"2019-11-14T06:43:19.664Z","address":"100,
The National Media Centre, Sector 24, Gurugram, Haryana 122022,
India","city":"Gurugram","createdAt":"2019-11-14T06:01:17.166Z"},{"_id":"5dccedacff47e867a2833801","tel":"XXXX","loc":[28.498692,77.095215],"tripId":"5dccedaaff47e867a28337ec","mode":"automated","osm_data":{"distance_remained":10791,"time_remained":1649.5},"distance_remained":10870,"time_remained":1173,"curr_ETA":"2019-11-14T06:43:18.459Z","address":"100,
The National Media Centre, Sector 24, Gurugram, Haryana 122022,
India","city":"Gurugram","createdAt":"2019-11-14T06:01:16.163Z"}]

I wanto access the data in this response.text as a dictionary

Rahul Sharma
  • 1,247
  • 2
  • 12
  • 33
  • `response.json` will give you a list of dicts, assuming the response is json-encoded. – snakecharmerb Nov 14 '19 at 09:20
  • Does this answer your question? [What's the best way to parse a JSON response from the requests library?](https://stackoverflow.com/questions/16877422/whats-the-best-way-to-parse-a-json-response-from-the-requests-library) – snakecharmerb Nov 14 '19 at 09:41

2 Answers2

2

This looks like a JSON string. You can parse it with json.loads()

>>> import json
>>> json.loads(response.text)

Assuming the response you are referring comes from using the requests library, you can also simply do response.json()

Daniel Hepper
  • 26,017
  • 8
  • 59
  • 69
0

This is simple:

import json

resp_text = request.text

dict=json.loads(res_text)

this will convert your response text in to dictionary

Yugandhar Chaudhari
  • 2,998
  • 2
  • 18
  • 33
vgp2018
  • 86
  • 4