How to Fix Broken JSON from ChatGPT and Other AI Tools?

You asked an AI tool for JSON. What you got back almost works, but your code can’t read it. This happens a lot when you build apps with LLMs. An LLM (large language model) is an AI that writes text, like ChatGPT.

The good news: there are only a few causes. You can fix them in three steps. Stop what you can. Check every reply. Then repair the rest.

Quick answer: Use your AI provider’s JSON mode or structured output if it has one. Then check each reply against a JSON Schema. If a reply is still broken, run it through a JSON repair tool. After that, check it again.

What usually goes wrong?

  • Extra text around the JSON. The reply starts with “Sure! Here’s the data you asked for:”. Then comes the object. Sometimes there is a short note at the end, too.
  • Markdown code fences. The JSON comes wrapped in ```json … ```. The model does this because it is used to writing answers for a chat window.
  • JavaScript or Python habits. You may see single quotes, keys without quotes, an extra comma at the end of a list, or comments. You may also see True, False and None instead of true, false and null.
  • Cut-off output. The reply hit the model’s output limit. So the JSON just stops. A string is not closed, and a few brackets are missing.
  • Valid JSON, wrong shape. Your code can read it. But a field is missing, has a different name, or has the wrong type.

Step 1: Stop the problem early

Use the provider’s structured output feature

Most LLM APIs now have a feature for this. It may be called structured output, JSON mode or tool calling. It forces the model to give valid JSON. Often it can also follow a JSON Schema that you send.

This removes most syntax problems before they start. So use it whenever you can.

Be clear in the prompt

Maybe you can’t use structured output. Then say exactly what you want. For example: “Respond with only a JSON object, no explanation and no code fences.”

After that, add an example of the exact shape you want. Examples work better than descriptions.

Leave room for the whole answer

Cut-off output happens when the model runs out of tokens. A token is a small piece of text, often part of a word. Guess how big the answer will be. Then set the maximum output tokens well above that number.

For long lists, ask for fewer items in each request. Then ask for the next part in a new request. The JSON Token Counter shows how many tokens a sample reply uses. It also shows how much you save by asking for minified JSON (JSON with no extra spaces).

Step 2: Check every reply

Never send model output straight into your app. First, parse it. To parse means to read the text and turn it into data.

Then check its structure against a JSON Schema. A schema lists the fields you need, their types and the values that are allowed.

If the check fails, treat it like any other bad input. Try again, use a backup plan, or report the error.

You can build and test your schema in the JSON Schema Validator. Or make a first draft from a good example with the Schema Generator.

Step 3: Repair what is still broken

A simple first step is to cut out the JSON before you parse it. If there is a code fence, take the part inside it. If not, start from the first { or [:

function extractJson(reply) {
  const fenced = reply.match(/```(?:json)?\s*([\s\S]*?)```/);
  const text = fenced ? fenced[1] : reply;
  const start = text.search(/[[{]/);
  return start === -1 ? text : text.slice(start);
}

This deals with chatty replies and code fences. But some problems need a real repair step. These include quotes, commas, Python words and cut-off output.

For these, you need a parser that accepts small mistakes. It should fix each problem and tell you what it changed.

Paste a broken reply into JSON Repair to see how this works. Here is what it does:

  • It removes the extra text and code fences.
  • It puts quotes around keys.
  • It changes single quotes to double quotes.
  • It changes Python words to JSON words.
  • It removes extra commas at the end of lists and objects.
  • It closes cut-off output.
  • It lists every fix it made.

Repair is a safety net. It does not replace checking. A repaired, cut-off reply is valid JSON, but some data is still missing. So always check the result against your schema before you use it.

A good order to follow

  1. Ask for structured output. If you can’t, use a strict prompt with an example.
  2. Try JSON.parse on the raw reply.
  3. If that fails, cut out the JSON and repair it. Write down what was repaired in your logs.
  4. Check the result against your JSON Schema.
  5. If the check fails, try once more. Put the error messages in the new prompt. If it still fails, stop and show a clear error.

Common questions

Why does ChatGPT add text around my JSON?

The model is trained to chat with people. So it often adds a friendly line before or after the data. Ask for “only a JSON object”, or use structured output, to stop this.

Why does the JSON from my AI tool stop in the middle?

The reply hit the output token limit. Set a higher limit, or ask for fewer items in each request.

Is repaired JSON safe to use?

Not on its own. Repair fixes the syntax, but data may still be missing or wrong. Always check the result against a JSON Schema first.

What is the best way to get valid JSON from an LLM?

Use the provider’s structured output or JSON mode, if it has one. It stops most syntax problems before they start.

Try it yourself

Free tools mentioned in this article — they run in your browser, so your data never leaves your device.

Related articles

All articles