Home > OS >  How to parse nested JSON object?
How to parse nested JSON object?

Time:01-02

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"
    }
  }
}

CodePudding user response:

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

CodePudding user response:

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
  • Related