# How to use the JSON API response

**URL:** <https://community.hubspot.com/t/how-to-use-the-json-api-response/57404>\
**Category:** APIs & Integrations\
**Tags:** python\
**Created:** [May 20, 2022, 3:26pm UTC](https://community.hubspot.com/t/how-to-use-the-json-api-response/57404 "2022-05-20T15:26:54Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![JKolb8](https://avatars.discourse-cdn.com/v4/letter/j/ecccb3/32.png) [@JKolb8](https://community.hubspot.com/u/JKolb8)\
**Post date:** [May 20, 2022, 3:26pm UTC](https://community.hubspot.com/t/how-to-use-the-json-api-response/57404/1 "2022-05-20T15:26:54Z")

</div>

Hallo,  
I try to accsess some data via an api call and Python:

“client.crm.companies.basic\_api.get\_page()”

In the documentation its written that the response is a json file.

When i try to save the response as a jason file I encounter several errors.

Im not experienced when it comes to the use of json so I might miss something here.  
First I tried to save the response with json.dump() wich led to several serialization errors. I tried some workarounds but none of them solved the problem and everything got far to complicated. Shouldnt it be possbile to just save the api response as a JSON file with a simple command.

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [May 20, 2022, 8:54pm UTC](https://community.hubspot.com/t/how-to-use-the-json-api-response/57404/2 "2022-05-20T20:54:45Z")

</div>

@taran42 has quickly become a Python wizard and may be able to lend a hand 😜

---

<div class="post-metadata">

**Author:** ![taran42](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/taran42/32/105524_2.png) [@taran42](https://community.hubspot.com/u/taran42)\
**Post date:** [May 20, 2022, 9:09pm UTC](https://community.hubspot.com/t/how-to-use-the-json-api-response/57404/3 "2022-05-20T21:09:36Z")

</div>

json.dump() is exactly how I saved the JSON data from the Hubspot API. I think I used something like

```lia-code-sample
with open('companies.json', 'w') as f:
 sys.stdout = f
 print(json.dumps(companies_list))
 sys.stdout = original_stdout

```

---

<div class="post-metadata">

**Author:** ![JKolb8](https://avatars.discourse-cdn.com/v4/letter/j/ecccb3/32.png) [@JKolb8](https://community.hubspot.com/u/JKolb8)\
**Post date:** [May 24, 2022, 1:24pm UTC](https://community.hubspot.com/t/how-to-use-the-json-api-response/57404/4 "2022-05-24T13:24:16Z")

</div>

Unfortunately it still dosent work i get the same error message.

```lia-code-sample
api_response = client.crm.companies.basic_api.get_page(limit=100, properties=["hs_lastmodifieddate","name","id","hs_created_by_user_id"], archived=False)
 print(type(api_response))

 out_file = open("companies.json", "w") 
 print(json.dumps(api_response)) 
 out_file.close()

```

I also tried it with your syntax but the problem is the same.

```lia-code-sample
 with open('companies.json', 'w') as f:
 sys.stdout = f
 print(json.dumps(api_response))
 #sys.stdout = original_stdout

```

and Here is the Error message:

TypeError: Object of type CollectionResponseSimplePublicObjectWithAssociationsForwardPaging is not JSON serializable

I also tried to save es as a txt file so I can access the Data and it looks like JSON syntax but with some formation errors.

---

<div class="post-metadata">

**Author:** ![taran42](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/taran42/32/105524_2.png) [@taran42](https://community.hubspot.com/u/taran42)\
**Post date:** [May 24, 2022, 2:41pm UTC](https://community.hubspot.com/t/how-to-use-the-json-api-response/57404/5 "2022-05-24T14:41:09Z")

</div>

It looks like Python is having trouble with your custom class. Given the error, take a look at [How to make a class JSON serializable](https://stackoverflow.com/questions/3768895/how-to-make-a-class-json-serializable) and [Make a Python Class JSON Serializable](https://pynative.com/make-python-class-json-serializable/) .

---

<div class="post-metadata">

**Author:** ![JKolb8](https://avatars.discourse-cdn.com/v4/letter/j/ecccb3/32.png) [@JKolb8](https://community.hubspot.com/u/JKolb8)\
**Post date:** [May 24, 2022, 2:48pm UTC](https://community.hubspot.com/t/how-to-use-the-json-api-response/57404/6 "2022-05-24T14:48:30Z")

</div>

But im just using the API response not my own Python Class.  
Why cant I just take the api response that already should be a json file and save it somewehre?

---

<div class="post-metadata">

**Author:** ![JKolb8](https://avatars.discourse-cdn.com/v4/letter/j/ecccb3/32.png) [@JKolb8](https://community.hubspot.com/u/JKolb8)\
**Post date:** [May 24, 2022, 2:52pm UTC](https://community.hubspot.com/t/how-to-use-the-json-api-response/57404/7 "2022-05-24T14:52:10Z")

</div>

I looked up your suggestions, but how should I edit a class that comes from Hubspot.  
class ‘hubspot.crm.companies.models.collection\_response\_simple\_public\_object\_with\_associations\_forward\_paging.CollectionResponseSimplePublicObjectWithAssociationsForwardPaging’

---

<div class="post-metadata">

**Author:** ![LKiesewetter](https://avatars.discourse-cdn.com/v4/letter/l/9fc29f/32.png) [@LKiesewetter](https://community.hubspot.com/u/LKiesewetter)\
**Post date:** [September 7, 2022, 1:04pm UTC](https://community.hubspot.com/t/how-to-use-the-json-api-response/57404/8 "2022-09-07T13:04:48Z")

</div>

Have you received an update on your problem or were you able to solve it yourself? I am currently facing the same issue and would be thankful for some input 🙂

---

<div class="post-metadata">

**Author:** ![DDebortoli](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/ddebortoli/32/196574_2.png) [@DDebortoli](https://community.hubspot.com/u/DDebortoli)\
**Post date:** [October 8, 2022, 10:07pm UTC](https://community.hubspot.com/t/how-to-use-the-json-api-response/57404/9 "2022-10-08T22:07:34Z")

</div>

Hi, i used this solution:

try:

api\_response = client.crm.companies.basic\_api.get\_page(limit=1, archived=False)

except ApiException as e:

print(“Exception when calling basic\_api-\>get\_page: %s\n” % e)

return api\_response.\_\_dict\_\_  
and used as a dictionary, worked fine to me

---

<div class="post-metadata">

**Author:** ![S-M-Ammar](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@S-M-Ammar](https://community.hubspot.com/u/S-M-Ammar)\
**Post date:** [November 23, 2022, 4:24pm UTC](https://community.hubspot.com/t/how-to-use-the-json-api-response/57404/10 "2022-11-23T16:24:21Z")

</div>

Thanks alot !!!

Worked for me too.

🙂

---

<div class="post-metadata">

**Author:** ![JuanOsorio4](https://avatars.discourse-cdn.com/v4/letter/j/f08c70/32.png) [@JuanOsorio4](https://community.hubspot.com/u/JuanOsorio4)\
**Post date:** [June 7, 2023, 11:58pm UTC](https://community.hubspot.com/t/how-to-use-the-json-api-response/57404/11 "2023-06-07T23:58:05Z")

</div>

Hi, I used this solution

try:

```
        **contacts** = api\_client.crm.contacts.basic\_api.get\_page(archived=False)

```

list\_contacts = **contacts.\_results #this return the results obtained inside a list.**

print(contact)

return list\_contacts

except ApiException as e:

return {

“error”: f"{e}"

}

---

<div class="post-metadata">

**Author:** ![dimam](https://avatars.discourse-cdn.com/v4/letter/d/e9c0ed/32.png) [@dimam](https://community.hubspot.com/u/dimam)\
**Post date:** [November 30, 2023, 1:26pm UTC](https://community.hubspot.com/t/how-to-use-the-json-api-response/57404/12 "2023-11-30T13:26:05Z")

</div>

No need to create custom serialization classes (and obviously you can’t change hubspot api classes as well), as others suggested!

All you have to do, is to search for a _to\_dict_ function inside the desired class, e.g. for [PublicUser](https://github.com/HubSpot/hubspot-api-python/blob/f736ceeaad533136f19b8b35d1aaf7bf19300b84/hubspot/settings/users/models/public_user.py#L24C7-L24C17) class I’ve searched (and found) [to\_dict function.](https://github.com/HubSpot/hubspot-api-python/blob/f736ceeaad533136f19b8b35d1aaf7bf19300b84/hubspot/settings/users/models/public_user.py#L183)  
**This function is there for any model in hubspot api!**

Code sample showcasing how to serialize the results list (where item in my example is of a [PublicUser](https://github.com/HubSpot/hubspot-api-python/blob/f736ceeaad533136f19b8b35d1aaf7bf19300b84/hubspot/settings/users/models/public_user.py#L24C7-L24C17) class type):

`match self.hubspot_object: case "USERS": write_ndjson_to_s3(hubspot_dump_s3_path, list(map(lambda item: json.dumps(item.to_dict()), results)), custom_serialized=True) # noqa: E501 case _: write_ndjson_to_s3(hubspot_dump_s3_path, results)`
