-1

I have some JSON data from the HubSpot CRM API which, after doing some pagination using Python Code essentially looks like this:

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

... and now what I'd like to do is:

1) Read one "set" of JSON at a time (meaning dealId, portalId, isDeleted)
2) If isDeleted==false then grab the dealId and portalId and store in variables
3) Use the variables from #2 above to build a URL string that can be used to go back to the HubSpot API and get information on each individual deal (this API endpoint is https://api.hubapi.com/deals/v1/deal/23487870?hapikey=demo (where 23487870 is the dealId from the above JSON)
4) Combine that individual deal-level info into another set of JSON. Specifically I want to grab /properties/dealname/value and properties/dealstage/value from JSON that looks like this:

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

5) And then output the final result in JSON something like this:

{
    "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": []
        }
    ]
}

... all in Python.

Any help?

Community
  • 1
  • 1
gotmike
  • 1,515
  • 4
  • 20
  • 44

1 Answers1

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)
Ananth
  • 4,227
  • 2
  • 20
  • 26
  • thanks... this is close, but i'm getting an error on the `if deal['isDeleted']:` line -- `TypeError: string indices must be integers, not str` – gotmike Mar 12 '16 at 04:35
  • Inside the for loop, add `print(deal)` and check if the dict you are expecting is in the right format. This error occurs if `deal` is not a dictionary, but is a string. – Ananth Mar 12 '16 at 07:41