-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,632
  • 3
  • 19
  • 49

2 Answers2

3

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
  • 27,965
  • 9
  • 68
  • 73
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
  • 3,469
  • 2
  • 22
  • 36
vgp2018
  • 86
  • 4