APIs & Integrations

JKolb8
Participant

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. 


11 Replies 11
dimam
Member

How to use the JSON API response

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)
0 Upvotes
JuanOsorio4
Participant

How to use the JSON API response

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}"
            }
0 Upvotes
DDebortoli
Member

How to use the JSON API response

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

 

S-M-Ammar
Participant

How to use the JSON API response

Thanks alot !!!

Worked for me too.

🙂

0 Upvotes
taran42
Contributor

How to use the JSON API response

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
JKolb8
Participant

How to use the JSON API response

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. 

0 Upvotes
taran42
Contributor

How to use the JSON API response

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 .

0 Upvotes
JKolb8
Participant

How to use the JSON API response

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?

0 Upvotes
JKolb8
Participant

How to use the JSON API response

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'

 

0 Upvotes
LKiesewetter
Participant

How to use the JSON API response

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 🙂

0 Upvotes
dennisedson
HubSpot Product Team
HubSpot Product Team

How to use the JSON API response

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