## TL;DR
The agent built the note payload by string concatenation and produced invalid JSON. Serialize the payload with a real JSON library, validate the string before sending, and log the raw body on failure so the next malformed payload is diagnosable in seconds.

```text
hubspot engagements API 'Invalid input JSON' from agent's malformed note payload
```

## Steps
1. Capture the exact request body the agent sent and run it through a JSON parser locally. Find the position the parser reports.
   Expected: the parser points at the exact broken spot (a stray quote, a missing comma, an unescaped newline).
2. Replace any hand-built JSON strings in the agent's code with the standard JSON serializer for its language. Never concatenate JSON by hand.
   Expected: the serializer produces valid output for the same note content.
3. Add a pre-send validation step: parse the serialized string and confirm the required engagement fields (type, body, associations) are present before the HTTP call.
   Expected: malformed payloads are caught before they reach HubSpot.
4. Resend the note with the serialized payload.
   Expected: the engagements API returns a 200 with an engagement id.
5. Add the raw request body to the error log whenever a JSON parse error comes back, so future failures carry their evidence.
   Expected: the next malformed payload is fixed from the log alone.

## Use this when
- The engagements API returns 'Invalid input JSON' on note, email, call, or task creates.
- The payload includes user-generated text (note bodies with quotes or newlines).
- The agent assembles request bodies with string templates.

## Not for this skill when
- The error is a validation error on field values rather than a JSON parse error.
- The request fails with 401, 403, or 429.
- Other endpoints accept the agent's payloads fine (then it is endpoint-specific schema, not JSON).

## Variant phrasings
- "hubspot note create invalid json"
- "engagements api json parse error from agent"
- "malformed engagement payload hubspot"

## Why it happens
Note bodies are free text full of quotes, backslashes, and newlines. When the agent interpolates that text into a JSON template string instead of letting a serializer escape it, the result is not valid JSON. HubSpot's parser rejects it before any business logic runs.

## Edge cases
- Emoji and other multi-byte characters are fine for serializers but break naive byte-level truncation. Truncate by character, then serialize.
- Some serializers escape forward slashes or unicode by default; that is still valid JSON and HubSpot accepts it.
- If the payload is valid JSON but HubSpot still complains, the problem is schema (missing required association), not syntax.

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_b-4WEtby2iNGdjlVcFEe-Q
