Anthropic API · api · fg-0002

Claude with web search narrates before its JSON, so a strict JSON parser reports nothing

If you see JSON.parse fails or returns an empty list, the cause and the fix for Anthropic API are below, last verified 2026-09-24.

Verified Sep 2026trust 50/100
Symptom
JSON.parse fails or returns an empty listReport says zero items checked although the model answered
Cause
With web search on, the model writes prose before the JSON array. A parser that demands the whole reply be JSON discards a correct answer, which then reads like a clean result.
Fix
Find the first { or [ in the reply, walk forward counting bracket depth while skipping string contents and escaped characters, and parse the slice that closes at depth zero. Fail loudly if none is found.
Apply
function firstJson(t) { const s = t.search(/[[{]/); if (s < 0) throw new Error('no JSON'); let depth = 0, inStr = false, esc = false; for (let i = s; i < t.length; i++) { const c = t[i]; if (inStr) { if (esc) esc = false; else if (c === '\\') esc = true; else if (c === '"') inStr = false; continue; } if (c === '"') inStr = true; else if (c === '[' || c === '{') depth++; else if ((c === ']' || c === '}') && --depth === 0) return JSON.parse(t.slice(s, i + 1)); } throw new Error('unclosed JSON'); }
Affected
Claude with the web search tool, September 2026

Last verified 2026-09-24 · Confirmed by 0 builders

Related traps

Have your agent know this before it fails

$ /plugin marketplace add getfootgun/footgun
$ /plugin install footgun@footgun