API - Create Engagements - Error Message

Hi this is the first time I have done this. I’ve created a test account and trying to add an engagement to an existing company. And I get the following error.

Status code: 400
Response: {“status”:“error”,“message”:“Invalid input JSON on line 1, column 15: Can not deserialize instance of com.hubspot.engagements.base.detail.views.EngagementView out of START_ARRAY token”,“correlationId”:“43b0088a-9991-4980-abe0-3e39750dbaab”,“requestId”:“db0d3584988ae0b441a650cae2d5e38c”}

Below is the JSON I’m passing.

{“engagement”:[{“property”:“active”,“value”:“True”},{“property”:“ownerId”,“value”:“31308819”},{“property”:“type”,“value”:“NOTE”},{“property”:“timestamp”,“value”:1517514219}],“associations”:[{“property”:“contactIds”,“value”:“2”},{“property”:“companyIds”,“value”:“686724768”}],“metadata”:[{“property”:“body”,“value”:“Import Record Test”}]}

Any help would be appreciated.

Thanks,

Ali

aliknox:

Invalid input JSON on line 1, column 15: Can not deserialize instance

Is my JSON file structure incorrect?

Hi @aliknox,

You’re correct; the engagement, associations, and metadata fields aren’t arrays, they’re objects. The request body should look like this:

{
 "engagement": {
 "active": true,
 "ownerId": 1,
 "type": "NOTE",
 "timestamp": 1409172644778
 },
 "associations": {
 "contactIds": [
 2
 ],
 "companyIds": [],
 "dealIds": [],
 "ownerIds": []
 },
 "attachments": [
 {
 "id": 4241968539
 }
 ],
 "metadata": {
 "body": "note body"
 }
 }

developers.hubspot.com

Create an Engagement | Engagements API

POST /engagements/v1/engagements - Create an engagement via the HubSpot API.

Thanks, I have resolved this, it was an array conversion problem.

Please could you explain how you resolved as I am having the same issue?

I found my own solution to this issue and will post it here as I have received a couple of upvotes to my initial question. Hopefully it will help someone else.

var createjsonVar = [];

createjsonVar.push({
 engagement: {
 active: true,
 type: "TASK",
 timestamp: new Date().getTime(,
 ownerId: ownerId
 },
 associations: {
 contactIds: [contactID]
 },
 metadata: {
 subject: 'follow up',
 body: taskBody,
 status: 'NOT_STARTED'
 }
});

var createjson = JSON.stringify(createjsonVar);
var body = createjson.substr(1, createjson.length - 2);

By taking a substring of the output you are removing the initial opening square bracket which causes the error.

I know it’s probably not the best solution but it works!

Thanks for posting your solution, @jaywhy !

I ran into a similar issue and it was caused by having ownerId in an array, which it shouldn’t be in