使用偏移量迭代Python中的JSON

2024-09-28 22:33:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用HubSpot CRM API获取“所有交易”。在

API终结点是:https://api.hubapi.com/deals/v1/deal/all?hapikey=demo

返回的JSON如下所示。。。在

{
    "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
}

我知道如果hasMore==true,那么您应该获取offset,并将其包含在另一个API调用中,类似这样:https://api.hubapi.com/deals/v1/deal/all?hapikey=demo&offset=1467187

然后一直这样做直到hasMore==false。在

我使用以下代码从API中提取第一个JSON块:

^{pr2}$

所以。。。我的问题是,现在我得到了我的JSON,如何:

1)读一块JSON
2) 如果hasMore==true,则再次执行1
3) ElseIfhasMore==false然后将上述所有迭代的JSON合并成一个大JSON
4) 从#3返回值

有什么帮助吗?在


Tags: apijsonfalsetruesourcevaluenullsalesforce
1条回答
网友
1楼 · 发布于 2024-09-28 22:33:03

工作溶液

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))

相关问题 更多 >