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!
LVL 9
diperspAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

clockwatcherCommented:
A first grade look would be to understand that arrays are indexed by number and dictionaries by keys.   So, simplifying things down to this first:

    a = [ {'name': 'bill'}, {'name': 'bob'} ]

What you've got in the above is an array ([]) of two dictionaries ({}).  And you''d index into it as such:

    a[0]['name']  

Where a[0] is the first element of the array-- the first dictionary:

    {'name': 'bill'}.  

And a[0]["name"] is the value 'bill' for the key called 'name'.

Your example is slightly more complex as it's an array of dictionaries with values that are themselves arrays of dictionaries.  But the idea is the same.  

So, assuming,

  data = r.json()

data[0] represents the first dictionary in your array.  In other words, it's the first client.:
{
  "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
  } ]
}

Open in new window

You index into a dictonary with it's keys.  So, data[0]['name'] is the same as in my earlier example.  You'd get back the value for the 'name' key which in your example would be 'client1'.  Taking it a step farther,

data[0][''domains'] is an array of dictionaries, so...

data[0]['domains'][0] is the first dictionary in that array-- the first domain.

data[0]['domains'][0]['name'] is the value ( 'domain1.com') for the 'name' key of that first domain.

data[0]['domains'][0]['deliveryDestinations'] is again an array of dictionaries, so you'd take it down a step farther...

data[0]['domains'][0]['deliveryDestinations'][0]['destination'] is the value for the destination key of the first deliveryDestination of the first domain of the first client.

Make sense?  That'd be a first grade look at things.  

In practice, you're rarely going to manually index into a data structure by hand.  The number of items is going to be dynamic.  Instead, you're going to iterate over the structure.  That's more of a second grade topic as it involves a looping construct but here's a look at that:
for client in data:
    for domain in client["domains"]:
        for destination in domain["deliveryDestinations"]:
            print client["name"]
            print domain["name"]
            print destination["destination"]
            print

Open in new window

The above iterates over each of the clients in your top level array.  For each of those clients, you're iterating over the domains.  And, for each of the domains, you're iterating over the destinations and printing out the client name, the domain name, and the destination.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mark BradyPrincipal Data EngineerCommented:
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
0
diperspAuthor Commented:
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!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.