Search code examples
jsonlistpython-2.7getresponse

How to treat response objects in python?


Ok i got a response using the reply command

reply = requests.get(http://api.eve-central.com/api/route/from/Jita/to/New Caldari

I have trouble dealing with the recieved data, isolating the data i want. How can get for example the value of the to: region: regionid: path?

This is what i get by using print reply.json() (reply.json()'s type is list):

[{u'to':
{u'region':
{u'regionid': 10000002, u'name': u'The Forge'},
u'security': 1.0, u'systemid': 30000145, u'name': u'New Caldari', u'constellationid': 20000020},
u'from':
{u'region':
{u'regionid': 10000002, u'name': u'The Forge'},
u'security': 0.9, u'systemid': 30000142, u'name': u'Jita', u'constellationid': 20000020}, u'secChange': False}]

Solution

  • Your JSON data is a list([]) that contains dictionaries. To access list elements, you use my_list[index], where index starts at 0. To access dictionary elements, you use my_dict[key], where key is the key of the dictionary's element you want to get.

    To access the "to: region: regionid"-path, you'd do the following:

    to_region_id = reply.json()[0]['to']['region']['regionid']