Extracting data from JSON and iterating using python
<p>So I'm using the HubSpot API which tracks "deals" in the system, but fails to have a robust way to search/filter the data it returns. So it simply dumps ALL the "deals" out of the system into one big JSON and then gives you some pagination info to help you glue it back together on the back-end.</p>

<p>Here is a demo/source API URL:
<a href="https://api.hubapi.com/deals/v1/deal/all?hapikey=demo" rel="nofollow">https://api.hubapi.com/deals/v1/deal/all?hapikey=demo</a></p>

<p>This returns JSON which looks basically like this (shortened):</p>

<pre><code>{  
   "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
}
</code></pre>

<p>but...</p>

<p>1) There are lots of deals returned, I'd like to filter this by <code>dealtype</code> which is a variable that can change. For instance, I'd like to return only deals that have the value "qualifiedtobuy"</p>

<p>2) I then need to run a query and "do stuff" with each deal, based on the <code>dealId</code>. I imagine this means I need to get all of the above into a <code>dict</code> and somehow and then iterate? I'm not sure.</p>

<p>So this is what I have so far, but this really just grabs the JSON and turns it into  <code>dict</code> (I think).</p>

<pre><code>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)
</code></pre>

<p>I'm not sure what steps to take next to grab the <code>dealId</code> values and then "do stuff" with each of the values returned.</p>

<p>Any help is much appreciated.</p>

<p>UPDATE: here is the raw dict, not simplified for the PROPERTIES for ONE DEAL:</p>

<pre><code>{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'}}
</code></pre>