Not able to get the custom property value from Ticket API response in Python

I’m try to get the value of the custom property that i’ve added in the Ticket properties. I can able to get the json response from the Ticket APi and display all of the ticket number(objectId) but when i try to display the value of the custom property that i created i’m getting “KeyError: ‘ref_num’”
Json response:
{‘portalId’: 43680158, ‘objectType’: ‘TICKET’, ‘objectId’: 1888262026, ‘properties’: {‘ref_num’: {‘versions’: [{‘name’: ‘ref_num’, ‘value’: ‘423011’, ‘timestamp’: 1694045255743, ‘sourceId’: ‘2069329’, ‘source’: ‘INTEGRATION’, ‘sourceVid’: [], ‘requestId’: ‘6514e376-91b6-4080-9786-2245a30d04bb’, ‘useTimestampAsPersistenceTimestamp’: True}], ‘value’: ‘423011’, ‘timestamp’: 1694045255743, ‘persistenceTimestamp’: 1694045255743, ‘source’: ‘INTEGRATION’, ‘sourceId’: ‘2069329’, ‘updatedByUserId’: None}}, ‘isDeleted’: False}

{‘portalId’: 43680158, ‘objectType’: ‘TICKET’, ‘objectId’: 1888290979, ‘properties’: {‘ref_num’: {‘versions’: [{‘name’: ‘ref_num’, ‘value’: ‘423009’, ‘timestamp’: 1694045254773, ‘sourceId’: ‘2069329’, ‘source’: ‘INTEGRATION’, ‘sourceVid’: [], ‘requestId’: ‘5d051f4d-3eb8-4950-b08f-6db274dec6d0’, ‘useTimestampAsPersistenceTimestamp’: True}], ‘value’: ‘423009’, ‘timestamp’: 1694045254773, ‘persistenceTimestamp’: 1694045254773, ‘source’: ‘INTEGRATION’, ‘sourceId’: ‘2069329’, ‘updatedByUserId’: None}}, ‘isDeleted’: False}

My code (Display ticket number (working)

url = "https://api.hubapi.com/crm-objects/v1/objects/tickets/paged?properties=ref_num"response = request("get", url)jsonTickets = response.json()for ticket in jsonTickets['objects']: print(format(ticket['objectId']))**********Displays:18882620261888290979*********

My code (Display ref_num (not - working)

url = "https://api.hubapi.com/crm-objects/v1/objects/tickets/paged?properties=ref_num"response = request("get", url)jsonTickets = response.json()for ticket in jsonTickets['objects']: print(format(ticket['ref_num']))******Error: print(format(ticket['ref_num']))~~~~~~^^^^^^^^^^^^^^^KeyError: 'ref_num'**********

How do i get the value of “ref_num” to display. My apologies if i’m new to python. Appreciate the help. Thanks

Hi, @BBTBCD :waving_hand: Thanks for the great question! I am not a regular Python user, and I’m happy to help.

Thought one — I wonder if this error happening because the property is nested one level deeper in properties. Maybe try adjusting your code like this? (Stack Overflow post I referenced)

for ticket in jsonTickets['objects']:
 print(format(ticket['properties']['ref_num']['value']))

Additionally, have you looked into implanting the Python Dictionary get() Method? This would allow you to account for when there is no value present for a requested object. It will return either the value for the property or a default value, like `None`, for cases where the property is blank or null. And help prevent potential KeyErrors in the future.

Have fun building! — Jaycee

Hey Jaycee

I just tried it and it’s PERFECT !!! Really appreciate the help.
Thanks