Link to home
Start Free TrialLog in
Avatar of dipersp
diperspFlag for United States of America

asked on

Manipulating requests.get response

I'm just learning python to extract data from a vendor's API.  I have an example from them to get some data, but that's as far as I can get.  I'm getting a response, but I'm trying to figure out how to parse the response.  I know just enough general programming to be dangerous.  I'm definitely in the beginner classification.

Here's part of the code that deals with my request.  Everything else in the code is just authenticating and such.  If there's something else you need to see, let me know.

# Get list of customers
r = requests.get(enterpriseURL + '/customers?fields=name,domains', headers=headers)

# print results
print 'Response status: {0}\n{1}'.format(r.status_code, r.text)

Open in new window


And here's a sample response -

Response status: 200
[ {
  "name" : "Client1",
  "domains" : [ {
    "name" : "domain1.com",
    "deliveryDestinations" : [ {
      "destination" : "10.10.10.10",
      "port" : 25,
      "preference" : 10
    }, {
      "destination" : "10.10.10.11",
      "port" : 25,
      "preference" : 20
    } ],
    "isPrimary" : true
  }, {
    "name" : "domain2.com",
    "deliveryDestinations" : [ {
      "destination" : "10.10.10.10",
      "port" : 25,
      "preference" : 10
    }, {
      "destination" : "10.10.10.11",
      "port" : 25,
      "preference" : 20
    } ],
    "isPrimary" : false
  } ]
}, {
  "name" : "Client2",
  "domains" : [ {
    "name" : "domain1.com",
    "deliveryDestinations" : [ {
      "destination" : "192.168.1.1",
      "port" : 25,
      "preference" : 10
    } ],
    "isPrimary" : true
  } ]

Open in new window


What I'm trying to do is manipulate that response.  I'd like to be able to pull out the name (Client1, Client2) and their corresponding domain names (domain1.com, etc.)

I read about dictionaries and appears I was able to get the response in to a dictionary and can output some of the info, but still can't figure out how to manipulate it.  

This was the code I used to play with this -

data = r.json()
print data[0]

Open in new window


Would appreciate some first grade programming help.  Thanks!
ASKER CERTIFIED SOLUTION
Avatar of clockwatcher
clockwatcher

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mark Brady
The response is a json string which you can load into a python dictionary and iterate over the keys and values. First, import the json module.

import json
import sys

response = '[{"name" : "Client1", "domains" : [ {"name" : "domain1.com","deliveryDestinations" : [ {"destination" : "10.10.10.10", "port" : 25, "preference" : 10}, {"destination" : "10.10.10.11", "port" : 25,"preference" : 20} ], "isPrimary" : true}, {"name" : "domain2.com", "deliveryDestinations" : [ {"destination" : "10.10.10.10", "port" : 25, "preference" : 10}, { "destination" : "10.10.10.11", "port" : 25, "preference" : 20} ],"isPrimary" : false} ]}, {"name" : "Client2", "domains" : [ {"name" : "domain1.com", "deliveryDestinations" : [ {"destination" : "192.168.1.1", "port" : 25, "preference" : 10 } ], "isPrimary" : true}]}]'
try:
	mydict = json.loads(response)	
except Exception as e:
	print e

# now you have a dictionary you can work with
result = []
for client in mydict:    
	temp = {'client': client['name'], 'domains': []}
	for domain in client['domains']:
		temp['domains'].append(domain['name'])

	result.append(temp)

# print your new list out as a valid json
print json.dumps(result)

Open in new window


Depending on how you want to use the output after processing it you could leave it as a list of dictionaries or dump it out as a modified json
Avatar of dipersp

ASKER

clockwatcher -

That's EXACTLY what I needed.  Clear and concise and extremely helpful.  With the last section of code you sent over, I've already begun manipulating the data.  Just knowing the syntax for accessing the data was a huge help!  Thanks!