# how do i convert 'SimplePublicObjectWithAssociations'

**URL:** <https://community.hubspot.com/t/how-do-i-convert-simplepublicobjectwithassociations/64205>\
**Category:** APIs & Integrations\
**Created:** [August 31, 2022, 6:33pm UTC](https://community.hubspot.com/t/how-do-i-convert-simplepublicobjectwithassociations/64205 "2022-08-31T18:33:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![karien](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/karien/32/9776_2.png) [@karien](https://community.hubspot.com/u/karien)\
**Post date:** [August 31, 2022, 6:33pm UTC](https://community.hubspot.com/t/how-do-i-convert-simplepublicobjectwithassociations/64205/1 "2022-08-31T18:33:44Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Jaycee\_Lewis](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jaycee_lewis/32/61352_2.png) [@Jaycee\_Lewis](https://community.hubspot.com/u/Jaycee_Lewis)\
**Post date:** [September 1, 2022, 8:34pm UTC](https://community.hubspot.com/t/how-do-i-convert-simplepublicobjectwithassociations/64205/2 "2022-09-01T20:34:49Z")

</div>

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

Thank you! Jaycee

---

<div class="post-metadata">

**Author:** ![LeeBartelme](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/leebartelme/32/95647_2.png) [@LeeBartelme](https://community.hubspot.com/u/LeeBartelme)\
**Post date:** [September 1, 2022, 8:39pm UTC](https://community.hubspot.com/t/how-do-i-convert-simplepublicobjectwithassociations/64205/3 "2022-09-01T20:39:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![karien](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/karien/32/9776_2.png) [@karien](https://community.hubspot.com/u/karien)\
**Post date:** [September 1, 2022, 9:05pm UTC](https://community.hubspot.com/t/how-do-i-convert-simplepublicobjectwithassociations/64205/4 "2022-09-01T21:05:21Z")

</div>

`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 itimport hubspotimport osimport jsonclient = 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 valuedetails = get_first_last_name_by_contact_id()print(details) `

---

<div class="post-metadata">

**Author:** ![karien](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/karien/32/9776_2.png) [@karien](https://community.hubspot.com/u/karien)\
**Post date:** [September 3, 2022, 6:41am UTC](https://community.hubspot.com/t/how-do-i-convert-simplepublicobjectwithassociations/64205/5 "2022-09-03T06:41:33Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![karien](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/karien/32/9776_2.png) [@karien](https://community.hubspot.com/u/karien)\
**Post date:** [September 3, 2022, 10:09am UTC](https://community.hubspot.com/t/how-do-i-convert-simplepublicobjectwithassociations/64205/6 "2022-09-03T10:09:21Z")

</div>

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 quotesapi_string = api_string.replace('None', "1")#to avoid none errorprint(type(api_string))print(api_string)json_data = json.loads(api_string)print(type(json_data))print(json_data)`

---

<div class="post-metadata">

**Author:** ![karien](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/karien/32/9776_2.png) [@karien](https://community.hubspot.com/u/karien)\
**Post date:** [September 3, 2022, 10:13am UTC](https://community.hubspot.com/t/how-do-i-convert-simplepublicobjectwithassociations/64205/7 "2022-09-03T10:13:38Z")

</div>

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)`

---

<div class="post-metadata">

**Author:** ![Klapprodt](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/klapprodt/32/15151_2.png) [@Klapprodt](https://community.hubspot.com/u/Klapprodt)\
**Post date:** [February 8, 2023, 8:14am UTC](https://community.hubspot.com/t/how-do-i-convert-simplepublicobjectwithassociations/64205/8 "2023-02-08T08:14:47Z")

</div>

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](https://d2qpr2jzcazej7.cloudfront.net/original/3X/5/f/5fcc94fcfab6f799742938b0e62c3d4dc40b90a5.png)

---

<div class="post-metadata">

**Author:** ![karien](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/karien/32/9776_2.png) [@karien](https://community.hubspot.com/u/karien)\
**Post date:** [March 18, 2023, 10:25pm UTC](https://community.hubspot.com/t/how-do-i-convert-simplepublicobjectwithassociations/64205/9 "2023-03-18T22:25:31Z")

</div>

ah thx for sharing.

---

<div class="post-metadata">

**Author:** ![Klapprodt](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/klapprodt/32/15151_2.png) [@Klapprodt](https://community.hubspot.com/u/Klapprodt)\
**Post date:** [March 20, 2023, 1:48pm UTC](https://community.hubspot.com/t/how-do-i-convert-simplepublicobjectwithassociations/64205/10 "2023-03-20T13:48:32Z")

</div>

You’re welcome. Happy to help 👍
