How to use the JSON API response

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.

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

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

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

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

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.

 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.

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 and Make a Python Class JSON Serializable .

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?

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’

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 :slightly_smiling_face:

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

Thanks alot !!!

Worked for me too.

:slightly_smiling_face:

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}"

}

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 class I’ve searched (and found) to_dict function.
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 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)