Skip to main content
AI JSONMedic
claude-opus-4 · claude-sonnet-4 · claude-haiku-4

Fix Claude AI JSON Errors

Claude consistently wraps JSON in markdown code fences even when instructed not to. Combined with truncation at token limits, this causes JSON.parse() failures in production. Here's the complete fix guide.

5 Claude JSON Error Patterns

1

Markdown wrapper (most frequent)

Claude Output

```json
{
  "result": "success",
  "data": [1, 2, 3]
}
```

After Repair

{
  "result": "success",
  "data": [1, 2, 3]
}

Fix: Strips ```json, ~~~, and any prose before/after the JSON block

2

Prose prefix before JSON

Claude Output

Here is the JSON output:
{
  "name": "Alice"
}

After Repair

{
  "name": "Alice"
}

Fix: Detects and removes natural language prefixes before the JSON object

3

Truncated response (token limit)

Claude Output

{"users": [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bo

After Repair

{"users": [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bo"}]}

Fix: Closes unclosed strings, arrays, and objects to produce valid JSON

4

Trailing comma in last field

Claude Output

{
  "name": "Alice",
  "active": true,
}

After Repair

{
  "name": "Alice",
  "active": true
}

Fix: Removes trailing commas in objects and arrays

5

Comment in JSON output

Claude Output

{
  "result": "ok", // success
  "code": 200
}

After Repair

{
  "result": "ok",
  "code": 200
}

Fix: Removes // and /* */ comments from JSON without touching string content

System Prompt Template (Reduce Errors)

Use this system prompt pattern to dramatically reduce markdown wrapping from Claude:

System Prompt Template
You are a JSON extraction assistant.

Return ONLY valid JSON. No markdown. No code fences. No explanation.
No trailing text after the closing brace.

The JSON object must have exactly these fields:
{
  "name": "string",
  "score": number,
  "tags": ["string"]
}

User input: {user_input}

Even with this prompt, always wrap JSON.parse() in try/catch and use the AI JSONMedic API as a fallback.

Production Pattern: Anthropic SDK + Repair Fallback

TypeScript (Anthropic SDK)
import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic();

async function getStructuredData(userInput: string) {
  const message = await client.messages.create({
    model: 'claude-sonnet-4-6',
    max_tokens: 1024,
    system: `Return ONLY valid JSON. No markdown fences. No explanation.
The response must be a JSON object with: name (string), score (number), tags (array of strings).`,
    messages: [{ role: 'user', content: userInput }],
  });

  const content = message.content[0].text;

  // First try: direct parse
  try {
    return JSON.parse(content);
  } catch {
    // Fallback: repair with AI JSONMedic API
    const repairRes = await fetch('https://aijsonmedic.com/api/repair', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ input: content }),
    });
    const { output, valid } = await repairRes.json();
    if (valid) return JSON.parse(output);
    throw new Error('Claude JSON output could not be repaired');
  }
}

Claude Model JSON Reliability

ModelJSON AccuracySpeedBest for
claude-opus-4-7HighestSlowestComplex schemas, long JSON
claude-sonnet-4-6HighFastProduction workloads
claude-haiku-4-5GoodFastestSimple structures, high volume

FAQ

Why does Claude return JSON with markdown code fences?

Claude defaults to wrapping code in markdown fences for readability. Even when you ask for "JSON only", Claude may still add ```json wrappers. Using system prompts to strictly forbid markdown, or using the Anthropic API with controlled output format, reduces this.

Does Claude support structured JSON output like OpenAI?

Claude does not have a native JSON mode equivalent to OpenAI JSON mode as of mid-2026. The recommended approach is to use a structured system prompt ("Return only valid JSON, no markdown, no explanation") and use AI JSONMedic as a repair fallback.

What Claude models produce the most reliable JSON?

Claude Opus 4 produces the most consistent JSON format adherence. Claude Sonnet 4 is a good balance of speed and accuracy. Claude Haiku 4 is fastest but more prone to format drift on complex schemas.

Related Guides

Fix Claude JSON Output Now

Paste your broken Claude response. Free, no signup, browser-side processing.

Open JSON Repair Tool →