Six ways model output breaks JSON
- A chatty wrapper: “Sure! Here is the data:” followed by a ```json fence and a closing remark.
- A trailing comma after the last item, as in {"a": 1, "b": 2,} — legal in JavaScript, not in JSON.
- Python values: True, False and None instead of true, false and null, often from a model that was just writing Python.
- Single or curly quotes, such as {'name': ‘Rina’}, which happen when the text passed through a word processor or chat app.
- A raw line break inside a string value. JSON needs \n written out instead.
- A reply that stops mid-object because it reached its max output tokens, leaving brackets and a string open.
Each of these makes JSON.parse or json.loads throw, even though the data a person reads is clear. The repair step fixes all six and reports which ones it found, so you can see whether the model needs a better prompt.
A JSON validator tells you; a repair tool changes the text
A validator, checker or linter only answers one question: does this parse, and if not, at which line and column does it fail? That is the right tool when you wrote the JSON by hand and want to fix it yourself. Repair goes further and rewrites the text into valid JSON. Use repair for machine output you did not write, and read the list of fixes before trusting the result. To find a single typo in a config file, the JSON Formatter points at the exact position instead.
Repair JSON inside your own code
This page uses the open-source jsonrepair library. The same fixes are available in your code, so a pipeline does not crash on one bad reply:
// JavaScript: npm install jsonrepair
import { jsonrepair } from 'jsonrepair'
const data = JSON.parse(jsonrepair(reply))
# Python: pip install json-repair
import json_repair
data = json_repair.loads(reply)
Log every reply that needed repair. A sudden rise usually means a prompt change or a new model version, and it is cheaper to catch that early than to debug missing fields later.
Ask for valid JSON in the first place
- OpenAI: use structured outputs with a json_schema response format and strict set to true.
- Anthropic: define a tool with an input_schema and ask Claude to call it, so the arguments arrive as JSON.
- Gemini: set responseMimeType to application/json and pass a responseSchema.
- Any model: raise the output token limit for long lists, and ask for the JSON only, with no explanation around it.
Even with these settings, keep the schema check. Valid syntax does not mean the right fields: a model can still return a string where your code expects a number, or leave out a required key. The validation step on this page catches exactly that.