API Create Contacts using Python passing a variable

I am using the CREATE contact method and using Python.

Everything works when I paste the json in the input section (

BatchInputSimplePublicObjectInputForCreate(inputs=[paste here]). I want to read a json file and pass an array of 30 records or read through the json file and pass one record at a time. When I create a variable, as a string or as a json object, i am getting an “Invalid input JSON” error. Is there any documentation or examples of how to pass a variable to the method?

Full Error:

HTTP response body: {“status”:“error”,“message”:“Invalid input JSON on line 1, column 12: Cannot deserialize value of type `java.util.ArrayList<com.hubspot.inbounddb.publicobject.core.v2.SimplePublicObjectInputForCreate>` from String value (token `JsonToken.VALUE_STRING`)”,“correlationId”.

Line of Code:

BatchInputSimplePublicObjectInputForCreate(inputs=jsonVar)

Hi @GeorgeNarr

I wanted to kindly request the JSON data that you are currently passing into the inputs. This information would greatly assist me with your mentioned problem.

Thanks!

I sent the json file but it might have been too long. I sent another abbreviated version. Here is the cod that processes the json file to be imported into HubSpot as contacts.

batchOf30String = ‘{ “properties”: {“rock_person_id”: “954”, “firstname”: “Dawn”, … “zip”: “30019-2810”}}’

# The above line will be replaced with reading the json string from a json file

batchJson = json.dumps(batchOf30String)

client = hubspot.Client.create(access_token=“code here”)

batch_input_simple_public_object_input_for_create = BatchInputSimplePublicObjectInputForCreate(inputs=batchJson)

try:

api_response = client.crm.contacts.batch_api.create(batch_input_simple_public_object_input_for_create=batch_input_simple_public_object_input_for_create)

pprint(api_response)

except ApiException as e:

print(“Exception when calling batch_api->create: %s\n” % e)

errorFile = e

In the variable i have tried square brackets and without the brackets. I also tried to send the data as a string and not dump as json.

Hi @GeorgeNarr

Please use this json. I hope this would help to resolve your query.

{
 "inputs": [
 {
 "properties": {
 "email": "email Address",
 "phone": "Phone number",
 "company": "Company",
 "website": "Website",
 "lastname": "Last Name",
 "firstname": "FirstName"
 }
 }
 ]
}

I hope this will help you out. Please mark it as Solution Accepted and upvote to help another Community member.
Thanks!

Maybe I don’t have the syntax exactl right. I am still getting the error:

HTTP response body: {"status":"error","message":"Invalid input JSON on line 1, column 12: Cannot deserialize value of type java.util.ArrayList<com.hubspot.inbounddb.publicobject.core.v2.SimplePublicObjectInputForCreate>from String value (tokenJsonToken.VALUE_STRING)","correlationId":"b6a25b6a-7ff1-45a4-9f51-ad3e44ba01c2"}

# ******** New Code Starts **************

tempRecords = ‘{“inputs”: [{ “properties”: {“rock_person_id”: “3592”, “firstname”: … “state”: “FL”, “zip”: “33470-4631”}}]}’

tempRecordsAsJson = json.dumps(tempRecords)

# ******** New Code Ends ***************

batch_input_simple_public_object_input_for_create = BatchInputSimplePublicObjectInputForCreate(tempRecordsAsJson)

To convert the string that I am reading from a json file (The file has 5 lines. Each line is a string of json like the above that represents 30 records). The reason for the file structure is taht there are character limitations in SQL and a limit of 150 reocrds per 10 seconds in the API i am consuming. The json conversion line of code is:

batchJson = json.dumps(batchOf30String)

Then I pass the variable “batchJson”. I tried this as a straignt string as well. I cannot seem to loacte the documentaiton for the HubSpot method to see the syntax. Thanks,

The answer is to use json.loads and not json.dumps. Here is the working solution.

tempRecords = ‘[{ “properties”: {“rock_person_id”: “5324”, “firstname”: “Julie”, … “state”: “FL”, “zip”: “33411-3321”}}]’

tempRecordsAsJson = json.loads(tempRecords)

batch_input_simple_public_object_input_for_create = BatchInputSimplePublicObjectInputForCreate(inputs=tempRecordsAsJson)