HubSpot API v5.3.0 deal creation throws unexpected JSON error

Hi everyone,

I’m stuck with an old PHP API version for HubSpot (v5.3.0) and can’t upgrade right now. I’ve been creating deals fine before, but suddenly I’m getting a weird JSON error.

My code looks like this:

$result = $hubspot->deals()->create($props, $assocs);

The $hubspot object is a working SevenShores\Hubspot\Http\Client. $props has stuff like ‘dealname’ => ‘Test’, and $assocs has ‘associatedCompanyIds’ => ‘123456789’.

Now I’m getting this error:

Invalid input JSON on line 1, column 342: Trailing token (of type END_OBJECT) found after value (bound as DealView): not allowed as per DeserializationFeature.FAIL_ON_TRAILING_TOKENS

I’m confused because the HubSpot class made the JSON. Any ideas on how to fix this? Thanks!

I’ve encountered a similar issue with the HubSpot API v5.3.0 when creating deals. The error you’re seeing typically stems from unexpected characters in the JSON payload, often caused by how the API constructs the request.

One workaround I found effective was to manually build the JSON payload instead of relying on the HubSpot class. Try something like this:

$payload = json_encode([
    'properties' => $props,
    'associations' => $assocs
]);

$endpoint = 'https://api.hubapi.com/deals/v1/deal';
$response = $hubspot->getClient()->request('POST', $endpoint, [
    'headers' => ['Content-Type' => 'application/json'],
    'body' => $payload
]);

This approach gives you more control over the JSON structure and might bypass the trailing token issue. Also, double-check your $props and $assocs arrays for any unexpected characters or formatting that could be throwing off the JSON encoding. Let me know if this helps!

Having dealt with HubSpot API v5.3.0 extensively, I can say this issue often stems from malformed JSON. While the previous suggestion of manually constructing the payload is valid, there’s another approach worth trying.

First, ensure your $props and $assocs arrays are clean. Sometimes hidden characters or unexpected data types can cause issues. Try using json_encode() with JSON_PRETTY_PRINT to inspect the payload:

echo json_encode(['properties' => $props, 'associations' => $assocs], JSON_PRETTY_PRINT);

This will help you spot any oddities. If everything looks good, try using the createOrUpdate() method instead:

$result = $hubspot->deals()->createOrUpdate($props, $assocs);

This method sometimes handles JSON construction differently and might bypass the issue you’re experiencing. If problems persist, consider reaching out to HubSpot support with your specific payload details. They might have insights into quirks with the older API version you’re using.

hey markseeker91, that error is annoying! i’ve run into it before. try cleaning up your $props and $assocs arrays - sometimes extra whitespace or weird characters can mess things up. also, double check you’re not accidentally adding any extra commas at the end of your arrays. if that doesn’t work, you might need to upgrade the api version unfortunately :frowning: