DEV Community

Azqora Tech
Azqora Tech

Posted on

Your LLM returned broken JSON. Here is how to repair it before JSON.parse throws

If you ask an LLM for JSON, you will sooner or later get something like this back:

Sure! Here is the data:

```json
{
  name: 'Asha',
  "active": True,
  "tags": ["admin", "beta",],
  "manager": None,
  "notes": "Joined in 2024"
```
Enter fullscreen mode Exit fullscreen mode

It looks like JSON. JSON.parse disagrees:

SyntaxError: Unexpected token 'S', "Sure! Here"... is not valid JSON
Enter fullscreen mode Exit fullscreen mode

Count the problems: chatty text before the data, a markdown code fence, an unquoted key, single quotes, Python's True and None, a trailing comma, and a missing closing brace because the response hit the token limit.

Here is how I deal with it in JavaScript, from cheapest fix to heaviest.

1. Always try the strict parse first

If the output is already valid, you want the real JSON.parse result, not a "repaired" guess.

function tryParse(text) {
  try {
    return { ok: true, value: JSON.parse(text) };
  } catch (err) {
    return { ok: false, error: err };
  }
}
Enter fullscreen mode Exit fullscreen mode

2. Cut out the JSON block

Most failures are just wrapping: a sentence before, a code fence around, an explanation after. Strip that first.

function extractJsonBlock(text) {
  // Prefer the contents of a ```
{% endraw %}
json fence if there is one
  const fence = text.match(/
{% raw %}
```(?:json)?\s*([\s\S]*?)(```
{% endraw %}
|$)/i);
  if (fence) text = fence[1];

  // Then keep everything from the first { or [ onwards
  const start = text.search(/[\[{]/);
  return start === -1 ? text.trim() : text.slice(start).trim();
}
{% raw %}

Enter fullscreen mode Exit fullscreen mode

This alone fixes a surprising share of real responses.

3. Repair the syntax

Trailing commas, single quotes, unquoted keys, True/None, comments and truncated endings are not worth hand-rolling regexes for. Regexes break as soon as a string value contains a comma or a quote. Use a proper tokenizer-based repairer such as jsonrepair:


js
import { jsonrepair } from 'jsonrepair';

function parseLlmJson(raw) {
  const first = tryParse(raw);
  if (first.ok) return first.value;

  const block = extractJsonBlock(raw);
  const second = tryParse(block);
  if (second.ok) return second.value;

  // Last resort: repair, then parse strictly
  return JSON.parse(jsonrepair(block));
}


Enter fullscreen mode Exit fullscreen mode

For the example above this gives:


json
{
  "name": "Asha",
  "active": true,
  "tags": ["admin", "beta"],
  "manager": null,
  "notes": "Joined in 2024"
}


Enter fullscreen mode Exit fullscreen mode

4. Validate the shape, not just the syntax

Repaired JSON is parseable, not necessarily correct. A truncated response may be missing whole fields. Check the result before you trust it:


js
const data = parseLlmJson(response);

if (typeof data.name !== 'string' || typeof data.active !== 'boolean') {
  throw new Error('LLM output parsed, but the shape is wrong');
}


Enter fullscreen mode Exit fullscreen mode

In a real project I'd use a schema validator (Zod, Ajv) here, and retry the request if validation fails.

5. Reduce how often it happens

  • Use your provider's JSON mode or structured outputs when available.
  • Put "Return only JSON, no explanation" in the prompt, and give one example object.
  • Raise the max token limit for large outputs, since truncation is the hardest break to repair honestly.

Quick manual check

When I just want to see what a broken response should have been, I paste it into a browser-based JSON Repair tool. It handles the same cases (missing brackets, trailing commas, Python dicts, LLM output) and runs locally, so API responses with customer data never leave the tab.

What is the weirdest "JSON" an LLM has handed you? Drop it in the comments.

Top comments (0)