APIs & Integrations

karien
Participante

how do i convert 'SimplePublicObjectWithAssociations'

resolver

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?

 

3 Solução aceitas
karien
Solução
Participante

how do i convert 'SimplePublicObjectWithAssociations'

resolver

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

 

 

Exibir solução no post original

0 Avaliação positiva
karien
Solução
Participante

how do i convert 'SimplePublicObjectWithAssociations'

resolver

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)

Exibir solução no post original

0 Avaliação positiva
Klapprodt
Solução
Colaborador(a)

how do i convert 'SimplePublicObjectWithAssociations'

resolver

I had the same problem.

The Hubspot objects have a method called ".to_dict()", that you can use. 
Just change them to a dict and then convert them to json. 

 

 

Klapprodt_0-1675843979184.png

 

Exibir solução no post original

0 Avaliação positiva
9 Respostas 9
Klapprodt
Solução
Colaborador(a)

how do i convert 'SimplePublicObjectWithAssociations'

resolver

I had the same problem.

The Hubspot objects have a method called ".to_dict()", that you can use. 
Just change them to a dict and then convert them to json. 

 

 

Klapprodt_0-1675843979184.png

 

0 Avaliação positiva
karien
Participante

how do i convert 'SimplePublicObjectWithAssociations'

resolver

ah thx for sharing. 

 

Klapprodt
Colaborador(a)

how do i convert 'SimplePublicObjectWithAssociations'

resolver

You're welcome. Happy to help 👍

LeeBartelme
HubSpot Employee
HubSpot Employee

how do i convert 'SimplePublicObjectWithAssociations'

resolver

Seeing the code you are having trouble with and an idea of the goal behind what you are doing would help diagnose the issue.

karien
Participante

how do i convert 'SimplePublicObjectWithAssociations'

resolver

 

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)  

 

0 Avaliação positiva
karien
Solução
Participante

how do i convert 'SimplePublicObjectWithAssociations'

resolver

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

 

 

0 Avaliação positiva
karien
Solução
Participante

how do i convert 'SimplePublicObjectWithAssociations'

resolver

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)
0 Avaliação positiva
karien
Participante

how do i convert 'SimplePublicObjectWithAssociations'

resolver

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)

 

0 Avaliação positiva
Jaycee_Lewis
Gerente da Comunidade
Gerente da Comunidade

how do i convert 'SimplePublicObjectWithAssociations'

resolver

Hi, @karien 👋 Thanks for reaching out. Hey @LeeBartelme @taran42, do y'all have any insight here for @karien?

 

Thank you! Jaycee 

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

0 Avaliação positiva