Iterating through JSON in Python using an OFFSET

2019-06-08 10:25发布

问题:

I am trying to use the HubSpot CRM API to get "All Deals".

The API endpoint is: https://api.hubapi.com/deals/v1/deal/all?hapikey=demo

The JSON returned looks like this...

{
    "deals": [
        {
            "portalId": 62515,
            "dealId": 18039629,
            "isDeleted": false,
            "associations": {
                "associatedVids": [],
                "associatedCompanyIds": [],
                "associatedDealIds": []
            },
            "properties": {
                "dealname": {
                    "value": "Company",
                    "timestamp": 1457040864519,
                    "source": "API",
                    "sourceId": null
                },
                "amount": {
                    "value": "10",
                    "timestamp": 1457040864519,
                    "source": "API",
                    "sourceId": null
                },
                "closedate": {
                    "value": "",
                    "timestamp": 1457040864519,
                    "source": "API",
                    "sourceId": null
                },
                "hubspot_owner_id": {
                    "value": "11626092",
                    "timestamp": 1457046177648,
                    "source": "SALESFORCE",
                    "sourceId": null
                },
                "hs_lastmodifieddate": {
                    "value": "1457046177662",
                    "timestamp": 1457046177662,
                    "source": "CALCULATED",
                    "sourceId": null
                },
                "hubspot_owner_assigneddate": {
                    "value": "1457046177648",
                    "timestamp": 1457046177648,
                    "source": "SALESFORCE",
                    "sourceId": null
                },
                "num_associated_contacts": {
                    "value": "0",
                    "timestamp": 0,
                    "source": "CALCULATED",
                    "sourceId": null
                },
                "hs_createdate": {
                    "value": "1457040864535",
                    "timestamp": 1457040864535,
                    "source": null,
                    "sourceId": null
                },
                "createdate": {
                    "value": "1457040864535",
                    "timestamp": 1457040864535,
                    "source": null,
                    "sourceId": null
                },
                "hs_salesforceopportunityid": {
                    "value": "00628000007nRyuAAE",
                    "timestamp": 1457046177648,
                    "source": "SALESFORCE",
                    "sourceId": null
                }
            },
            "imports": []
        },
        {
            "portalId": 62515,
            "dealId": 18040854,
            "isDeleted": false,
            "associations": {
                "associatedVids": [],
                "associatedCompanyIds": [],
                "associatedDealIds": []
            },
            "properties": {
                "dealname": {
                    "value": "5678",
                    "timestamp": 1457042290572,
                    "source": "API",
                    "sourceId": null
                },
                "amount": {
                    "value": "750000.0",
                    "timestamp": 1457042290572,
                    "source": "API",
                    "sourceId": null
                },
                "closedate": {
                    "value": "",
                    "timestamp": 1457042290572,
                    "source": "API",
                    "sourceId": null
                },
                "hs_lastmodifieddate": {
                    "value": "1457042290592",
                    "timestamp": 1457042290592,
                    "source": "CALCULATED",
                    "sourceId": null
                },
                "num_associated_contacts": {
                    "value": "0",
                    "timestamp": 0,
                    "source": "CALCULATED",
                    "sourceId": null
                },
                "hs_createdate": {
                    "value": "1457042290592",
                    "timestamp": 1457042290592,
                    "source": null,
                    "sourceId": null
                },
                "createdate": {
                    "value": "1457042290592",
                    "timestamp": 1457042290592,
                    "source": null,
                    "sourceId": null
                }
            },
            "imports": []
        }
    ],
    "hasMore": true,
    "offset": 1467187
}

And I understand that if hasMore==true, then you are supposed to grab the offset and include it in another API call something like this: https://api.hubapi.com/deals/v1/deal/all?hapikey=demo&offset=1467187

And then keep doing that until hasMore==false.

I am using the following code to extract the first chunk of JSON from the API:

import requests

url = "https://api.hubapi.com/deals/v1/deal/all"

querystring = {"hapikey":"demo"}

headers = {
    'cache-control': "no-cache"
    }

response = requests.request("GET", url, headers=headers, params=querystring)

print(response.text)

So... my question is that now I am getting my JSON, how do I:

1) Read one chunk of JSON
2) If hasMore==true then go do #1 again
3) ElseIf hasMore==false then combine ALL the JSON from ALL iterations of #1 above into one big JSON
4) Return the value from #3

Any help please?

回答1:

Working solution

import json
import requests

url = "https://api.hubapi.com/deals/v1/deal/all"

querystring = {"hapikey":"demo"}

headers = {
    'cache-control': "no-cache"
    }

all_deals = []

response = requests.request("GET", url, headers=headers, params=querystring).json()

for deal in response['deals']:
    all_deals.append(deal)

hasMore = response['hasMore']
offset = response['offset']

while hasMore:

    querystring = {
        "hapikey":"demo",
        "offset":offset
        }
    response = requests.request("GET", url, headers=headers, params=querystring).json()

    for deal in response['deals']:
        all_deals.append(deal)

    hasMore = response['hasMore']
    offset = response['offset']

print(json.dumps(all_deals))