因此,我使用 HubSpot API 来跟踪系统中的“交易”,但无法提供可靠的方法来搜索/过滤它返回的数据。因此,它只是将系统中的所有“交易”转储到一个大 JSON 中,然后为您提供一些分页信息,以帮助您在后端将其重新粘合在一起。
这里是演示/源 API URL: https://api.hubapi.com/deals/v1/deal/all?hapikey=demo
这会返回 JSON,它看起来基本上像这样(缩短):
{
"deals":[
{
"portalId":62515,
"dealId":17886969,
"isDeleted":false,
"associations":{
"associatedCompanyIds":[
113448746
]
},
"properties":{
"dealname":{
"value":"Google Website"
},
"amount":{
"value":"150000"
},
"hubspot_owner_id":{
"value":"72"
},
"dealstage":{
"value":"qualifiedtobuy"
},
"dealtype":{
"value":"existingbusiness"
}
},
"imports":[
]
}
],
"hasMore":false,
"offset":28692358
}
但是...
1) 返回了很多交易,我想通过 dealtype
进行过滤,这是一个可以更改的变量。例如,我只想返回值为“qualifiedtobuy”的交易
2) 然后,我需要运行查询并根据 dealId
对每笔交易“执行操作”。我想这意味着我需要将上述所有内容放入 dict
中,然后以某种方式进行迭代?我不确定。
这就是我到目前为止所拥有的,但这实际上只是获取 JSON 并将其转换为 dict
(我认为)。
import requests
import json
apikey = "demo"
url = 'https://api.hubapi.com/deals/v1/deal/all?hapikey=' + apikey
response = requests.get(url)
response.raise_for_status()
jsonDeals = response.json()
dict_object = dict(jsonDeals)
我不确定接下来要采取什么步骤来获取 dealId
值,然后对每个返回的值进行“处理”。
非常感谢任何帮助。
更新:这是原始字典,未针对一笔交易的属性进行简化:
{u'hs_lastmodifieddate': {u'sourceId': None, u'timestamp': 1457479099306, u'versions': [{u'timestamp': 1457479099306, u'sourceVid': [], u'name': u'hs_lastmodifieddate', u'value': u'1457479099306', u'source': u'CALCULATED'}], u'value': u'1457479099306', u'source': u'CALCULATED'}, u'pipeline': {u'sourceId': None, u'timestamp': 1457479063182, u'versions': [{u'timestamp': 1457479063182, u'name': u'pipeline', u'value': u'default', u'sourceVid': []}], u'value': u'default', u'source': None}, u'num_associated_contacts': {u'sourceId': None, u'timestamp': 0, u'versions': [{u'source': u'CALCULATED', u'name': u'num_associated_contacts', u'value': u'0', u'sourceVid': []}], u'value': u'0', u'source': u'CALCULATED'}, u'dealstage': {u'sourceId': None, u'timestamp': 1457479063157, u'versions': [{u'timestamp': 1457479063157, u'sourceVid': [], u'name': u'dealstage', u'value': u'qualifiedtobuy', u'source': u'API'}], u'value': u'qualifiedtobuy', u'source': u'API'}, u'createdate': {u'sourceId': None, u'timestamp': 1457479063181, u'versions': [{u'timestamp': 1457479063181, u'name': u'createdate', u'value': u'1457479063181', u'sourceVid': []}], u'value': u'1457479063181', u'source': None}, u'hs_salesforceopportunityid': {u'sourceId': None, u'timestamp': 1457479097680, u'versions': [{u'timestamp': 1457479097680, u'sourceVid': [], u'name': u'hs_salesforceopportunityid', u'value': u'00628000007nhqFAAQ', u'source': u'SALESFORCE'}], u'value': u'00628000007nhqFAAQ', u'source': u'SALESFORCE'}, u'hubspot_owner_assigneddate': {u'sourceId': None, u'timestamp': 1457479097680, u'versions': [{u'timestamp': 1457479097680, u'sourceVid': [], u'name': u'hubspot_owner_assigneddate', u'value': u'1457479097680', u'source': u'SALESFORCE'}], u'value': u'1457479097680', u'source': u'SALESFORCE'}, u'hubspot_owner_id': {u'sourceId': None, u'timestamp': 1457479097680, u'versions': [{u'timestamp': 1457479097680, u'sourceVid': [], u'name': u'hubspot_owner_id', u'value': u'11626092', u'source': u'SALESFORCE'}], u'value': u'11626092', u'source': u'SALESFORCE'}, u'amount': {u'sourceId': None, u'timestamp': 1457479063157, u'versions': [{u'timestamp': 1457479063157, u'sourceVid': [], u'name': u'amount', u'value': u'150000', u'source': u'API'}], u'value': u'150000', u'source': u'API'}, u'hs_createdate': {u'sourceId': None, u'timestamp': 1457479063181, u'versions': [{u'timestamp': 1457479063181, u'name': u'hs_createdate', u'value': u'1457479063181', u'sourceVid': []}], u'value': u'1457479063181', u'source': None}, u'salesforcelastsynctime': {u'sourceId': None, u'timestamp': 1457479099298, u'versions': [{u'timestamp': 1457479099298, u'sourceVid': [], u'name': u'salesforcelastsynctime', u'value': u'1457479070904', u'source': u'SALESFORCE'}], u'value': u'1457479070904', u'source': u'SALESFORCE'}, u'closedate': {u'sourceId': None, u'timestamp': 1457479099298, u'versions': [{u'timestamp': 1457479099298, u'sourceVid': [], u'name': u'closedate', u'value': u'1461013200000', u'source': u'SALESFORCE'}], u'value': u'1461013200000', u'source': u'SALESFORCE'}, u'dealtype': {u'sourceId': None, u'timestamp': 1457479063157, u'versions': [{u'timestamp': 1457479063157, u'sourceVid': [], u'name': u'dealtype', u'value': u'existingbusiness', u'source': u'API'}], u'value': u'existingbusiness', u'source': u'API'}, u'dealname': {u'sourceId': None, u'timestamp': 1457479063157, u'versions': [{u'timestamp': 1457479063157, u'sourceVid': [], u'name': u'dealname', u'value': u'Google Website', u'source': u'API'}], u'value': u'Google Website', u'source': u'API'}}
最佳答案
response.json() 返回的对象已转换为 dict
,因此您无需对其执行任何进一步操作。要获取所有qualifiedtobuy
交易的列表,请尝试如下操作:
jsonDeals = response.json()
deals = []
for deal in jsonDeals['deals']:
properties = deal['properties']
if ('dealstage' in properties and
properties['dealstage']['value'] == 'qualifiedtobuy'):
deals.append(deal)
if deals:
print(deals[0]['dealId'])
else:
print('found no "qualifiedtobuy" deals')
关于python - 从 JSON 中提取数据并使用 python 进行迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35809755/