We use cookies to make HubSpot's community a better place. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. To learn more, and to see a full list of cookies we use, check out our Cookie Policy (baked goods not included).
Aug 31, 2022 2:33 PM - edited Aug 31, 2022 2:35 PM
the JSON object must be str, bytes or bytearray, not 'SimplePublicObjectWithAssociations'
i tried with Json.. output looks Json or like python dict but obviously isn't?
Solved! Go to Solution.
Sep 3, 2022 2:41 AM - edited Sep 3, 2022 2:43 AM
ok i found my problem, i dont need to use json to convert this object into dict. so i can unpack it....
i simply need to use the response to get what i need... how easy is that, thats if you know....no conversions needed... no unpacking of nested dictionaries.... lists etc.
api_response = client.crm.objects.basic_api.get_by_id(object_type="0-1", object_id=f'{object_id_contact}',
properties=["firstname", "lastname"], archived=False)
firstname = api_response.properties["firstname"]
-->Karien
Sep 3, 2022 6:13 AM - edited Sep 3, 2022 6:25 AM
in another use case i did need to convert to dict. and had some errors, to correct those i did the following
api_response = client.crm.objects.associations_api.get_all(object_type="2-7431691", object_id=f'{object_id_sg}', to_object_type="0-1",
api_string = (str(api_response))
api_string = api_string.replace("'",'"')
api_string = api_string.replace('None', "1")
json_data = json.loads(api_string)
Sep 1, 2022 4:39 PM
Seeing the code you are having trouble with and an idea of the goal behind what you are doing would help diagnose the issue.
Sep 1, 2022 5:05 PM - edited Sep 1, 2022 5:43 PM
GOAL == get first and last name of objectid (contacts)
thx Lee, i condensed whats important in the code and pertaining to the issue.
I need to get the simplePublicObject* type returned converted to Dict.
to be able to unpack it
import hubspot
import os
import json
client = hubspot.Client.create(api_key="api_key")
#def main(event):
def get_first_last_name_by_contact_id():
#object_id_contact = event.get('inputFields').get('')
object_id_contact = "12468101"
OAUTH = os.getenv('OAUTH')
try:
api_response = client.crm.objects.basic_api.get_by_id(object_type="0-1",
object_id=f'{object_id_contact}',
properties=["firstname", "lastname"],
archived=False)
except ApiException as e:
print("Exception when calling basic_api->get_by_id: %s\n" % e)
return api_response -> 200
-> {"archived": False,
"archived_at": 1,
"associations": 1,
"created_at": datetime.datetime(2022, 6, 16, 11, 55, 49, 19000, tzinfo=tzutc()),
"id": "12468101",
"properties": {"createdate": "2022-06-16T11:55:49.019Z",
"firstname": "Karien",
"hs_object_id": "12468101",
"lastmodifieddate": "2022-09-01T09:43:44.443Z",
"lastname": "Skinner"},
"properties_with_history": 1,
"updated_at": datetime.datetime(2022, 9, 1, 9, 43, 44, 443000, tzinfo=tzutc())}
#below is what i tried amongst other things to try make json.load work.
api_string = (str(api_response))
api_string = api_string.replace("'",'"') #double quotes needed for json
#try to convert to dict error - i need to unpack my output to get first and last names
json_data = json.loads(api_string)
print(json_data) -> raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value
details = get_first_last_name_by_contact_id()
print(details)
Sep 3, 2022 2:41 AM - edited Sep 3, 2022 2:43 AM
ok i found my problem, i dont need to use json to convert this object into dict. so i can unpack it....
i simply need to use the response to get what i need... how easy is that, thats if you know....no conversions needed... no unpacking of nested dictionaries.... lists etc.
api_response = client.crm.objects.basic_api.get_by_id(object_type="0-1", object_id=f'{object_id_contact}',
properties=["firstname", "lastname"], archived=False)
firstname = api_response.properties["firstname"]
-->Karien
Sep 3, 2022 6:13 AM - edited Sep 3, 2022 6:25 AM
in another use case i did need to convert to dict. and had some errors, to correct those i did the following
api_response = client.crm.objects.associations_api.get_all(object_type="2-7431691", object_id=f'{object_id_sg}', to_object_type="0-1",
api_string = (str(api_response))
api_string = api_string.replace("'",'"')
api_string = api_string.replace('None', "1")
json_data = json.loads(api_string)
Sep 3, 2022 6:09 AM
and also a note, its for this specific case where i have access to the properties, in other case you do need to convert to dict --> (json)
if this can help someone else i used below to convert to dict for another use case
api_response = client.crm.objects.associations_api.get_all(object_type="2-7431691", object_id=f'{object_id_sg}', to_object_type="0-1",
api_string = (str(api_response))
api_string = api_string.replace("'",'"')#for json needs double quotes
api_string = api_string.replace('None', "1")#to avoid none error
print(type(api_string))
print(api_string)
json_data = json.loads(api_string)
print(type(json_data))
print(json_data)
Sep 1, 2022 4:34 PM
Hi, @karien 👋 Thanks for reaching out. Hey @LeeBartelme @taran42, do y'all have any insight here for @karien?
Thank you! Jaycee