DEV Community

Cover image for How to Clean Messy LLM JSON Output Without Regex Hell
M0N0S0DIUM
M0N0S0DIUM

Posted on • Originally published at textforge.co

How to Clean Messy LLM JSON Output Without Regex Hell

You ask an LLM for JSON. It returns:

{
  "name": "John Doe",
  "email": "john@example.com",
  "phone": "555-1234",
  "notes": "Customer called about billing issue -- needs follow-up",
  "tags": ["billing", "urgent"]
}
Enter fullscreen mode Exit fullscreen mode

Looks clean. But paste it into JSON.parse() and boom:

SyntaxError: Unexpected token } in JSON at position 147
Enter fullscreen mode Exit fullscreen mode

The culprit? Trailing comma after the last property. Or maybe it wrapped the whole thing in markdown code fences. Or used single quotes. Or included a comment. Or got cut off mid-token.
You reach for regex. replace(/,\s*}/g, '}') fixes trailing commas. But then you hit markdown fences. Then single quotes. Then a stray // comment. Then a truncated string. Each fix breaks something else.

There's a better way.

Why Existing Tools Fail

Tool Fails On
Regex Nested objects, escaped quotes, partial truncation
jq Invalid JSON (trailing commas, comments, single quotes)
Python json.loads() Strict — any violation throws
Online formatters Manual copy-paste, not automatable
Custom Python scripts Maintenance burden, edge cases

The pattern: every tool assumes valid JSON. LLM output is almost JSON.

The API Approach: One Call, Pipeline of Fixes

Instead of chaining fragile fixes, define a pipeline — ordered transforms that each handle one class of mess.

curl -X POST https://textforge.co/v1/run \
  -H "Content-Type: application/json" \
  -d '{
    "input": "{name: \"John\", email: \"john@example.com\",}",
    "pipeline": ["removespecial", "removemultiple"]
  }'
Enter fullscreen mode Exit fullscreen mode

Response:

{
  "success": true,
  "input": "{name: \"John\", email: \"john@example.com\",}",
  "pipeline": ["removespecial", "removemultiple"],
  "result": "{\"name\": \"John\", \"email\": \"john@example.com\"}",
  "steps": [
    {"step": 1, "action": "removespecial", "result": "{\"name\": \"John\", \"email\": \"john@example.com\"}"},
    {"step": 2, "action": "removemultiple", "result": "{\"name\": \"John\", \"email\": \"john@example.com\"}"}
  ],
  "execution_time_ms": 3
}
Enter fullscreen mode Exit fullscreen mode

Free tier: 1,000 requests/day. No API key. No account.

Top comments (0)