Reputation: 1192
I am pulling JSON data from a GET request over an API and I am unable to get the responses to join together. I am new to dictionaries and indexes (and python for that manner).
The API data is the same, it is just a different page number. Able to verify via the eclipse debugger. I am able to confirm that each API call is able to pull the data it needs as well.
#pulls from the API
response = requests.get(url, headers=headers)
obj_json = response.content
objLoader = json.loads(obj_json)
obj1 = objLoader
#url changes to page 2
response = requests.get(url, headers=headers)
obj_json = response.content
obj1 = json.loads(obj_json)
newObj = dict(obj1.items() + obj2.items())
#I have also tried the following:
#dict(obj1.items() | obj2.items())
In the end I'd like obj2 to be appended to obj1.
#Let's say obj1 has:
indexes[0][1]
#and obj2 has
indexes [0][1]
#I'd like obj1 to have
indexes[0][1][2][3]
The obj1 & 2 item data is structured like this:
obj1/2: dict u'count': 25370, u'items': [{..........}]
__len__: int: 508
count: int 25370
items: <type 'list'>: [{...}]
page: int
pages: int
Thank you, and let me know if you need more information!
Upvotes: 2
Views: 11074
Reputation: 2065
You can do it like that:
url_header_list = [
(url1, headers1),
(url2, headers2),
(url3, headers3),
...
]
items = []
# You can change your headers and url in any way you want, not just like that
for url, headers in url_header_list:
# And this you need to do for each pair of url and headers
response = requests.get(url, headers=headers).json()
items.extend(response['items'])
items
will contain all the items from each response.
Upvotes: 1
Reputation: 10058
Lets assume your JSON response looks like this:
{"ID":1,"name":"Donald","first-name":"Trump","age":25,"hobbies":["tweet","hunting",{"sports":["volley-ball","golf"]}],"address":{}}
For Obj2:
{"ID":2,"name":"Barack","first-name":"Obama","age":25,"hobbies":["reading","cinema",{"sports":["volley-ball","badminton"]}],"address":{}}
You need to convert the JSON response to dict, and then append this dictionary to an array of objects:
[{"ID":1,"name":"Donald","first-name":"Trump","age":25,"hobbies":["tweet","hunting",{"sports":["volley-ball","golf"]}],"address":{}},{"ID":2,"name":"Barack","first-name":"Obama","age":25,"hobbies":["reading","cinema",{"sports":["volley-ball","badminton"]}],"address":{}}]
Upvotes: 2