[Hubspot API] How do you use IN filter operator?

SOLVE
CSwart
Participant

I am trying to use an IN Filter for getting all deals from a list of deal_ids when trying this I get an invalid JSON error:

 

HTTP response body: {
"status":"error",
"message":"Invalid input JSON on line 1, column 42: Cannot deserialize instance of `java.lang.String` out of START_ARRAY token",
"correlationId":"dd4d7def-6cc8-4b7e-a48d-658e0330e8cd"
}

 


I am using the Python API as such to do the IN filter operation with a list of deal_id strings in deal_ids:

 

domain_filter = hubspot.crm.deals.models.Filter(
            property_name="id",
            operator="IN",
            value=deal_ids
        )

        filter_group = hubspot.crm.deals.models.FilterGroup(
            filters=[domain_filter]
        )

        public_object_search_request = hubspot.crm.companies.models.PublicObjectSearchRequest(
            filter_groups=[filter_group]
        )
        
        deal_results = self.hubspot_api_client.crm.companies.search_api.do_search(public_object_search_request=public_object_search_request)

 

 

This is what the dict repr of public_object_search_request looks like:

 

{'after': None,
 'filter_groups': [{'filters': [{'operator': 'IN',
                                 'property_name': 'id',
                                 'value': ['3651752129', '3741754830']}]}],
 'limit': None,
 'properties': None,
 'query': None,
 'sorts': None}

Would love to see a working IN operator example to get it working for myself.

0 Upvotes
1 Accepted solution

Accepted Solutions
Teun
Solution
Guide | Diamond Partner

Hi @CSwart ,

 

I checked the Deals docs, and I think you can retrieve the deals with two endpoints.

 

Batch read (inputs= should get your array of ID's):

import hubspot
from pprint import pprint
from hubspot.crm.deals import BatchReadInputSimplePublicObjectId, ApiException

client = hubspot.Client.create(api_key="YOUR_HUBSPOT_API_KEY")

batch_read_input_simple_public_object_id = BatchReadInputSimplePublicObjectId(properties="dealname", inputs=[{"id":"3651752129"},{"id":"3741754830"}])
try:
    api_response = client.crm.deals.batch_api.read(archived=False, batch_read_input_simple_public_object_id=batch_read_input_simple_public_object_id)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling batch_api->read: %s\n" % e)

 

Search:

import hubspot
from pprint import pprint
from hubspot.crm.deals import PublicObjectSearchRequest, ApiException

client = hubspot.Client.create(api_key="YOUR_HUBSPOT_API_KEY")

public_object_search_request = PublicObjectSearchRequest(filter_groups={"filters":[{"value":"3741754830","propertyName":"hs_object_id","operator":"EQ"},{"value":"3651752129","propertyName":"hs_object_id","operator":"EQ"}]}, sorts="dealname", query="string", properties="dealname", limit=10, after=0)
try:
    api_response = client.crm.deals.search_api.do_search(public_object_search_request=public_object_search_request)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling search_api->do_search: %s\n" % e)

OR:

import hubspot
from pprint import pprint
from hubspot.crm.deals import PublicObjectSearchRequest, ApiException

client = hubspot.Client.create(api_key="YOUR_HUBSPOT_API_KEY")

public_object_search_request = PublicObjectSearchRequest(filter_groups={"filters":[{"value":"[\"3651752129\", \"3741754830\"]","propertyName":"hs_object_id","operator":"IN"}]}, sorts="dealname", query="string", properties="dealname", limit=10, after=0)
try:
    api_response = client.crm.deals.search_api.do_search(public_object_search_request=public_object_search_request)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling search_api->do_search: %s\n" % e)

 



Did my answer solve your issue? Help the community by marking it as the solution.

View solution in original post

2 Replies 2
Teun
Solution
Guide | Diamond Partner

Hi @CSwart ,

 

I checked the Deals docs, and I think you can retrieve the deals with two endpoints.

 

Batch read (inputs= should get your array of ID's):

import hubspot
from pprint import pprint
from hubspot.crm.deals import BatchReadInputSimplePublicObjectId, ApiException

client = hubspot.Client.create(api_key="YOUR_HUBSPOT_API_KEY")

batch_read_input_simple_public_object_id = BatchReadInputSimplePublicObjectId(properties="dealname", inputs=[{"id":"3651752129"},{"id":"3741754830"}])
try:
    api_response = client.crm.deals.batch_api.read(archived=False, batch_read_input_simple_public_object_id=batch_read_input_simple_public_object_id)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling batch_api->read: %s\n" % e)

 

Search:

import hubspot
from pprint import pprint
from hubspot.crm.deals import PublicObjectSearchRequest, ApiException

client = hubspot.Client.create(api_key="YOUR_HUBSPOT_API_KEY")

public_object_search_request = PublicObjectSearchRequest(filter_groups={"filters":[{"value":"3741754830","propertyName":"hs_object_id","operator":"EQ"},{"value":"3651752129","propertyName":"hs_object_id","operator":"EQ"}]}, sorts="dealname", query="string", properties="dealname", limit=10, after=0)
try:
    api_response = client.crm.deals.search_api.do_search(public_object_search_request=public_object_search_request)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling search_api->do_search: %s\n" % e)

OR:

import hubspot
from pprint import pprint
from hubspot.crm.deals import PublicObjectSearchRequest, ApiException

client = hubspot.Client.create(api_key="YOUR_HUBSPOT_API_KEY")

public_object_search_request = PublicObjectSearchRequest(filter_groups={"filters":[{"value":"[\"3651752129\", \"3741754830\"]","propertyName":"hs_object_id","operator":"IN"}]}, sorts="dealname", query="string", properties="dealname", limit=10, after=0)
try:
    api_response = client.crm.deals.search_api.do_search(public_object_search_request=public_object_search_request)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling search_api->do_search: %s\n" % e)

 



Did my answer solve your issue? Help the community by marking it as the solution.

View solution in original post

CSwart
Participant

Thanks for the helpful solution ideas I am going to try them out today!