You ask a model for JSON and get something that almost parses. JSON.parse throws, your pipeline stops, and the error message points at character 1,742. Here's why it happens, how to repair a reply you already have, and how to stop it happening.
Why models return broken JSON
A language model writes text one token at a time. Unless the API forces a structure, nothing stops it from writing something that only looks like JSON. The usual problems:
- Extra text around it. "Here's the JSON you asked for:" before, an explanation after, or the whole thing wrapped in a
```jsoncode fence. - JavaScript or Python habits. Trailing commas, single quotes, unquoted keys, comments, or
True,False, andNoneinstead oftrue,false, andnull. - Unescaped characters. A real line break or a stray double quote inside a string value.
- Cut-off replies. The model hit its maximum output length mid-object, leaving unclosed brackets and strings.
Repair a reply you already have
Paste the whole reply into AI JSON Repair. It finds the JSON inside the surrounding text, fixes the problems above, and lists every change it made, so you can check nothing was guessed wrongly. A cut-off reply is closed so it parses, but the missing data can't be recovered. If a reply was cut off, raise the maximum output length and ask again.
Once it parses, the JSON Formatter makes it readable and points to the exact line of any remaining error.
Stop it happening: use structured outputs
The reliable fix is to have the API enforce the shape, instead of asking nicely in the prompt:
- OpenAI: Structured Outputs, by passing a JSON Schema with
strict: true. Replies then match the schema. - Claude: define a tool whose input schema is the shape you want and have the model call it, or use structured outputs where your model supports them.
- Gemini: set the response type to
application/jsonand give a response schema.
Each provider supports a slightly different part of JSON Schema. For example, OpenAI's strict mode requires every property to be listed as required and additionalProperties to be false. The AI JSON Schema Builder creates a schema from a sample of the JSON you want, and checks it against each provider's rules.
If you can't use structured outputs
- Show one complete example of the exact JSON you want in the prompt.
- Ask for JSON only: no explanation and no code fence.
- Leave enough room in the maximum output length for the whole reply.
- Parse defensively: extract the JSON, repair it, then validate it against your schema before using it.
- When a reply fails validation, send it back with the error and ask the model to fix it. One retry usually works.
Checklist
- Use the provider's structured outputs whenever you can.
- Keep the schema simple, and check it against the provider's rules.
- Repair, then validate. Never trust a repair without validating it.
- If replies get cut off, raise the output limit instead of repairing them.