Buster3650
Buster3650

Reputation: 478

How to merge json get requests using Python?

How do i merge multiple json get requests into one single json variable in Python?

The json looks like the following after a get request:

{
"collection": [{
     "name": "George Freeman",
     "address": "Hoverstreet 12"
      }, {
      "name": "Michael Hojob",
      "address": "Bandroad 12"
     }
   ]
}

Basically, what i am trying to do is to iterate in params {'skippages': i, 'pagesize': 1000} to fetch ALL data stored, and then save it all in one single json variable / file. Therefore, i need to append json everytime i iterate skippages. Question is is that done in a python programming way. Moreover, i need to only append the json objects, which means that "collection" should not be appended, but the API should look like this the second time that the get request has been executed:

    {
    "collection": [{
        "name": "George Freeman",
        "address": "Hoverstreet 12"
        }, {
        "name": "Michael Hojob",
        "address": "Bandroad 12"
        }, {
        "name": "Jack Jackson",
        "address": "Jacksonvile 12"
        }, {
        "name": "Mona Hilton",
        "address": "Readroad 12"
        }
    ]
}

This is somehow what i imagined the code should look like, but unfortunately it overwrites the json, and does not append, when executing following part of code. Also, it does not consider the fact that it only needs to append the json objects within "collection":

    for i in range(0, 6):
        r = requests.get(url="xxx", headers="xxx", params="xxx[i]").json() ##count params due to pagination. This works

Upvotes: 0

Views: 1257

Answers (3)

javelin_hurler
javelin_hurler

Reputation: 71

collection = {
    "collection" : []
}

for i in range(0,6):
    r = requests.get(url="xxx", headers="xxx", params="xxx[i]").json()
    collection["collection"].extend(r["collection"])

print(collection)

What happens is that collection["collection"] is the array in your collection object. Extend will append every item from another array (in this case r["collection"] which is one paghe from your request)

Upvotes: 1

Epsi95
Epsi95

Reputation: 9047

You can do something like

# initialize python dict
d = {
"collection": [
   ]
}


# next time 
result_1 = {
"collection": [{
     "name": "George Freeman",
     "address": "Hoverstreet 12"
      }, {
      "name": "Michael Hojob",
      "address": "Bandroad 12"
     }
   ]
}

d["collection"].extend(result_1['collection'])

# next time 
result_2 = {
"collection": [{
     "name": "George Freeman",
     "address": "Hoverstreet 12"
      }, {
      "name": "Michael Hojob",
      "address": "Bandroad 12"
     }
   ]
}

d["collection"].extend(result_2['collection'])



print(d)
{'collection': [{'name': 'George Freeman', 'address': 'Hoverstreet 12'},
  {'name': 'Michael Hojob', 'address': 'Bandroad 12'},
  {'name': 'George Freeman', 'address': 'Hoverstreet 12'},
  {'name': 'Michael Hojob', 'address': 'Bandroad 12'}]}

Upvotes: 1

buran
buran

Reputation: 14233

start with empty list and then extend it with data in response

data = {"collection":[]}

for i in range(6):
    response = requests.get(url="xxx", headers="xxx", params="xxx[i]") # count params due to pagination. This works
    data['collection'].extend(response.json().get('collection', []))

Upvotes: 2

Related Questions