-1

我有一些来自 HubSpot CRM API 的 JSON 数据,在使用 Python 代码进行一些分页之后,基本上看起来像这样:

[
  {
    "dealId": 18039629,
    "portalId": 62515,
    "isDeleted": false
  },
  {
    "dealId": 18040854,
    "portalId": 62515,
    "isDeleted": false
  }
]

...现在我想做的是:

1)一次读取一组“JSON”(意思是 dealId、portalId、isDeleted)
2)如果isDeleted==false然后抓取dealIdandportalId并存储在变量中
3)使用上面 #2 中的变量来构建一个 URL 字符串,该字符串可用于返回 HubSpot API 并获取有关每笔交易的信息(此 API 端点是https://api.hubapi.com/deals/v1/deal/23487870?hapikey=demo(上面的 JSON 来自哪里23487870dealId4
)将该单独的交易级别信息组合到另一组 JSON 中。具体来说,我想从如下所示的 JSON 中 获取/properties/dealname/value和获取:properties/dealstage/value

{
    "portalId": 62515,
    "dealId": 23487870,
    "isDeleted": false,
    "properties": {
        "dealname": {
            "value": "AutomationTestUser-national",
            "timestamp": 1457692022120
        },
        "dealstage": {
            "value": "appointmentscheduled",
            "timestamp": 1457692022120
        }
    },
    "imports": []
}

5) 然后以 JSON 格式输出最终结果,如下所示:

{
    "deals":
    [
        {
            "portalId": 62515,
            "dealId": 23487870,
            "isDeleted": false,
            "properties": {
                "dealname": {
                    "value": "AutomationTestUser-national",
                    "timestamp": 1457692022120
                },
                "dealstage": {
                    "value": "appointmentscheduled",
                    "timestamp": 1457692022120
                }
            },
            "imports": []
        },
        {
            "portalId": 62515,
            "dealId": 23487871,
            "isDeleted": false,
            "properties": {
                "dealname": {
                    "value": "AutomationTestUser-regional",
                    "timestamp": 1457692022120
                },
                "dealstage": {
                    "value": "appointmentscheduled",
                    "timestamp": 1457692022120
                }
            },
            "imports": []
        }
    ]
}

...全部在 Python 中。

有什么帮助吗?

4

1 回答 1

1
import json
output = {"deals": []}

data = """
[
  {
    "dealId": 18039629,
    "portalId": 62515,
    "isDeleted": false
  },
  {
    "dealId": 18040854,
    "portalId": 62515,
    "isDeleted": false
  }
]
"""

j = json.loads(data)
for deal in j:
    if deal['isDeleted']:
        continue              # Ignore if deleted
    dealId = deal['dealId']
    url = 'https://api.hubapi.com/deals/v1/deal/' + str(dealId) + '?hapikey=demo' # Construct new url here
    data = getSource(url) # implement getSource to perform web request and get data
    j2 = json.loads(data)
    output['deals'].append(j2)

print(output)
于 2016-03-12T04:16:23.127 回答