
Stop JSON.parse From Crashing on LLM Responses
I want to show you every way an LLM can break your JSON.parse call, and how to handle all of them in one line. The many ways LLMs break JSON If you're calling OpenAI, Claude, Gemini, Ollama, or any other LLM and asking for JSON, here's what you'll eventually get back: 1. Markdown code fences The most common one. You ask for JSON, the model gives you a helpful little markdown block: Sure! Here you go: ``` { % endraw % } json { "score" : 95 } { % raw % } **2. Trailing commas** Models love trailing commas. Especially in arrays. ```json {"items": ["a", "b", "c",], "count": 3,} 3. Unquoted keys The model writes JavaScript instead of JSON: { name: "Alice" , age: 30 , active: true } 4. Single quotes { 'name': 'Alice' , 'city': 'New York' } 5. Smart quotes and em dashes Copy-paste artifacts or just models being fancy: { "name" : "Alice" , "range" : "10—20" } Those " and — characters look right but they are not ASCII and JSON.parse will reject them. 6. Inline comments { "name" : "Alice" , //
Continue reading on Dev.to JavaScript
Opens in a new tab



