-1

I am working on a new project in HubSpot that returns nested JSON like the sample below. I am trying to access the associated contacts id, but am struggling to reference it correctly (the id I am looking for is the value '201' in the example below). I've put together this script, but this script only returns the entire associations portion of the JSON and I only want the id. How do I reference the id correctly?

Here is the output from the script:

{'contacts': {'paging': None, 'results': [{'id': '201', 'type': 'ticket_to_contact'}]}}

And here is the script I put together:

import hubspot
from pprint import pprint

client = hubspot.Client.create(api_key="API_KEY")

try:
    api_response = client.crm.tickets.basic_api.get_page(limit=2, associations=["contacts"], archived=False)

    for x in range(2):
        pprint(api_response.results[x].associations)
        
except ApiException as e:
    print("Exception when calling basic_api->get_page: %s\n" % e)

Here is what the full JSON looks like ('contacts' property shortened for readability):

{
  "results": [
    {
      "id": "34018123",
      "properties": {
        "content": "Hi xxxxx,\r\n\r\nCan you clarify on how the blocking of script happens? Is it because of any CSP (or) the script will decide run time for every URL’s getting triggered from browser?\r\n\r\nRegards,\r\nLogan",
        "createdate": "2019-07-03T04:20:12.366Z",
        "hs_lastmodifieddate": "2020-12-09T01:16:12.974Z",
        "hs_object_id": "34018123",
        "hs_pipeline": "0",
        "hs_pipeline_stage": "4",
        "hs_ticket_category": null,
        "hs_ticket_priority": null,
        "subject": "RE: call followup"
      },
      "createdAt": "2019-07-03T04:20:12.366Z",
      "updatedAt": "2020-12-09T01:16:12.974Z",
      "archived": false
    },
    {
      "id": "34018892",
      "properties": {
        "content": "Hi Guys,\r\n\r\nI see that we were placed back on the staging and then removed again.",
        "createdate": "2019-07-03T07:59:10.606Z",
        "hs_lastmodifieddate": "2021-12-17T09:04:46.316Z",
        "hs_object_id": "34018892",
        "hs_pipeline": "0",
        "hs_pipeline_stage": "3",
        "hs_ticket_category": null,
        "hs_ticket_priority": null,
        "subject": "Re: Issue due to server"
      },
      "createdAt": "2019-07-03T07:59:10.606Z",
      "updatedAt": "2021-12-17T09:04:46.316Z",
      "archived": false,
      "associations": {
        "contacts": {
          "results": [
            {
              "id": "201",
              "type": "ticket_to_contact"
            }
          ]
        }
      }
    }
  ],
  "paging": {
    "next": {
      "after": "35406270",
      "link": "https://api.hubapi.com/crm/v3/objects/tickets?associations=contacts&archived=false&hs_static_app=developer-docs-ui&limit=2&after=35406270&hs_static_app_version=1.3488"
    }
  }
}
martineau
  • 119,623
  • 25
  • 170
  • 301
MHalliday
  • 31
  • 4
  • This documentation may be useful https://realpython.com/python-json/ – msanford Jan 01 '22 at 22:35
  • @martineau, thanks, I had looked at some of this and I also have a 100days of Python course that covers it a bit, the challenge I have was how to access each object type. Going through each error, I was able to come up with the right answer, which is: api_response.results[x].associations["contacts"].results[0].id to get the first id – MHalliday Jan 01 '22 at 23:20
  • I think your last comment must have be to @msanford, not me. – martineau Jan 01 '22 at 23:39

3 Answers3

2

Sorted this out, posting in case anyone else is struggling with the response from the HubSpot v3 Api. The response schema for this call is:


    Response schema type: Object
    String  results[].id
    Object  results[].properties
    String  results[].createdAt
    String  results[].updatedAt
    Boolean results[].archived
    String  results[].archivedAt
    Object  results[].associations
    Object  paging
    Object  paging.next
    String  paging.next.after
    String  paging.next.linkResponse schema type: Object
    String  results[].id
    Object  results[].properties
    String  results[].createdAt
    String  results[].updatedAt
    Boolean results[].archived
    String  results[].archivedAt
    Object  results[].associations
    Object  paging
    Object  paging.next
    String  paging.next.after
    String  paging.next.link

So to access the id of the contact associated with the ticket, you need to reference it using this notation:

api_response.results[1].associations["contacts"].results[0].id

notes:

  • results[x] - reference the result in the index
  • associations["contacts"] - associations is a dictionary object, you can access the contacts item by it's name
  • associations["contacts"].results is a list - reference by the index []
  • id - is a string
MHalliday
  • 31
  • 4
1

In my case type was ModelProperty or CollectionResponseProperty couldn't reach dict anyhow.

For the record this got me to go through the results.

for result in list(api_response.results):
   ID = result.id
0

You can do api_response.results[x].associations["contacts"]["results"][0]["id"].

BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
  • Thanks, I've tried that but I get the error: pprint(api_response.results[x].associations["contacts"]["results"][0]["id"]) TypeError: 'NoneType' object is not subscriptable – MHalliday Jan 01 '22 at 22:43
  • What is the output of `type(api_response.results[x].associations)`? I had initially assumed it was a dictionary, but I could be wrong based on the fact that you're using dot notation. – BrokenBenchmark Jan 01 '22 at 23:15
  • 1
    the output was: . After going through all the type errors, I was able to sort it out. I'm new to this type of nesting, but looks like the key understanding what type I'm trying to reference – MHalliday Jan 01 '22 at 23:47