# API Create Contacts using Python passing a variable

**URL:** <https://community.hubspot.com/t/api-create-contacts-using-python-passing-a-variable/101784>\
**Category:** APIs & Integrations\
**Created:** [March 5, 2024, 9:05pm UTC](https://community.hubspot.com/t/api-create-contacts-using-python-passing-a-variable/101784 "2024-03-05T21:05:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![GeorgeNarr](https://avatars.discourse-cdn.com/v4/letter/g/a3d4f5/32.png) [@GeorgeNarr](https://community.hubspot.com/u/GeorgeNarr)\
**Post date:** [March 5, 2024, 9:05pm UTC](https://community.hubspot.com/t/api-create-contacts-using-python-passing-a-variable/101784/1 "2024-03-05T21:05:47Z")

</div>

I am using the CREATE contact method and using Python.

> **[Accounts Dashboard | HubSpot](https://app.hubspot.com/myaccounts?next=developerdocs&redirect=%2Fapi-reference%2Fcrm-contacts-v3%2Fguide)**

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)

---

<div class="post-metadata">

**Author:** ![GRajput](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/grajput/32/102020_2.png) [@GRajput](https://community.hubspot.com/u/GRajput)\
**Post date:** [March 6, 2024, 10:57am UTC](https://community.hubspot.com/t/api-create-contacts-using-python-passing-a-variable/101784/2 "2024-03-06T10:57:10Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![GeorgeNarr](https://avatars.discourse-cdn.com/v4/letter/g/a3d4f5/32.png) [@GeorgeNarr](https://community.hubspot.com/u/GeorgeNarr)\
**Post date:** [March 6, 2024, 10:32pm UTC](https://community.hubspot.com/t/api-create-contacts-using-python-passing-a-variable/101784/3 "2024-03-06T22:32:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![GeorgeNarr](https://avatars.discourse-cdn.com/v4/letter/g/a3d4f5/32.png) [@GeorgeNarr](https://community.hubspot.com/u/GeorgeNarr)\
**Post date:** [March 6, 2024, 10:40pm UTC](https://community.hubspot.com/t/api-create-contacts-using-python-passing-a-variable/101784/4 "2024-03-06T22:40:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![GRajput](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/grajput/32/102020_2.png) [@GRajput](https://community.hubspot.com/u/GRajput)\
**Post date:** [March 7, 2024, 8:15am UTC](https://community.hubspot.com/t/api-create-contacts-using-python-passing-a-variable/101784/5 "2024-03-07T08:15:12Z")

</div>

Hi @GeorgeNarr

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

```lia-code-sample
{
 "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!

---

<div class="post-metadata">

**Author:** ![GeorgeNarr](https://avatars.discourse-cdn.com/v4/letter/g/a3d4f5/32.png) [@GeorgeNarr](https://community.hubspot.com/u/GeorgeNarr)\
**Post date:** [March 7, 2024, 12:04pm UTC](https://community.hubspot.com/t/api-create-contacts-using-python-passing-a-variable/101784/6 "2024-03-07T12:04:02Z")

</div>

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 (token`JsonToken.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)

---

<div class="post-metadata">

**Author:** ![GeorgeNarr](https://avatars.discourse-cdn.com/v4/letter/g/a3d4f5/32.png) [@GeorgeNarr](https://community.hubspot.com/u/GeorgeNarr)\
**Post date:** [March 6, 2024, 3:04pm UTC](https://community.hubspot.com/t/api-create-contacts-using-python-passing-a-variable/101784/7 "2024-03-06T15:04:05Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![GeorgeNarr](https://avatars.discourse-cdn.com/v4/letter/g/a3d4f5/32.png) [@GeorgeNarr](https://community.hubspot.com/u/GeorgeNarr)\
**Post date:** [March 7, 2024, 5:35pm UTC](https://community.hubspot.com/t/api-create-contacts-using-python-passing-a-variable/101784/8 "2024-03-07T17:35:41Z")

</div>

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)
