dipersp
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.
And here's a sample response -
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 -
Would appreciate some first grade programming help. Thanks!
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)
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
} ]
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]
Would appreciate some first grade programming help. Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!
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!
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