# Deterministic Broken LLM JSON Auto-Repair & AST Healer API [API-382]
> Repair truncated LLM JSON, trailing commas, unclosed quotes, markdown fences, and Python True/False/None literals in sub-1ms without paying for an LLM retry.

## Specifications
- Canonical Documentation: https://topaigateway.com/api/json-surgeon
- Production Endpoint: https://topaigateway.com/v1/tollbooth/json-surgeon
- Method: POST
- Category: FinOps & Structured Output Reliability
- Pack / License: Cursor & Claude Code Harness ($49 Once) / Pack 1 DevEx ($0 7-Day Trial) ($49 one-time (or $0 7-Day Free Trial on Pack 1))
- Latency Target: < 1 ms
- Free Tier: 100 free requests/month (no credit card required)
- Direct Checkout: https://topaigateway.com/checkout?pack=bundle_cursor_harness&direct=true&utm_source=gsc_pseo&utm_medium=organic&utm_campaign=json-surgeon

## What's Included Inside Your Instant Download
- Production POST /v1/tollbooth/json-surgeon & /v1/api/382-json-surgeon sub-1ms Pushdown-Automaton RFC 8259 healer
- Deterministic recovery for truncated LLM streams, markdown fences, trailing commas, unclosed quotes & Python literals
- Bundled inside the $49 Cursor & Claude Code MCP Harness Kit (mcp.json, .cursorrules, 19 MCP tools, 169 harness files)
- Or start with Pack 1 DevEx ($0 7-Day Free Trial) + instant topai_live_* key & TypeScript/Python fallback wrappers

## Buyer Protection Guarantee
- 14-Day 100% No-Questions-Asked Money-Back Guarantee · Instant ZIP + API Key Delivery · Zero Recurring Fees on Toolkits

## Quickstart (cURL)
```bash
curl -X POST https://topaigateway.com/v1/tollbooth/json-surgeon \
  -H "Content-Type: application/json" \
  -d '{"broken_json": "{\"items\": [{\"id\": 1, \"active\": True, \"title\": \"Truncated LLM output"}'
```

## Sample Request
```json
{
  "broken_json": "```json\n{\"agent\": \"cursor-coder\", \"active\": True, \"nullable\": None, \"steps\": [{\"id\": 1, \"action\": \"unclosed string and trailing comma\",}"
}
```

## Sample Response
```json
{
  "status": "REPAIRED_RFC8259_VALID",
  "valid_json": true,
  "repaired_string": "{\"agent\": \"cursor-coder\", \"active\": true, \"nullable\": null, \"steps\": [{\"id\": 1, \"action\": \"unclosed string and trailing comma\"}]}",
  "parsed_object": {
    "agent": "cursor-coder",
    "active": true,
    "nullable": null,
    "steps": [
      {
        "id": 1,
        "action": "unclosed string and trailing comma"
      }
    ]
  },
  "llm_retry_cost_saved_usd": 0.03,
  "latency_ms": 0.42
}
```

## SDK Code Samples
### TypeScript / Node.js
```typescript
// Zero-LLM retry fallback when JSON.parse(llmOutput) throws SyntaxError
const res = await fetch("https://topaigateway.com/v1/tollbooth/json-surgeon", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ broken_json: rawLlmResponse })
});
const { parsed_object } = await res.json();
```

### Python
```python
import requests

# Also available via universal catalog route: /v1/api/382-json-surgeon
resp = requests.post(
    "https://topaigateway.com/v1/tollbooth/json-surgeon",
    json={"broken_json": '{"items": [{"id": 1, "active": True, "title": "Truncated'}
)
data = resp.json()
print("Valid RFC 8259 JSON:", data["valid_json"], data["parsed_object"])
```

## Performance Benchmarks
- Repair Latency: 0.4 ms
- LLM Retry Saved: $0.03 / call
- Engine Type: Pushdown AST
- RFC 8259 Recovery: 99.8% Success

## Compliance & Governance
- European Union DORA (UE) 2022/2554 Article 30 Compliant
- Zero-Retention & Zero-PII Policy: Prompts are executed on memory-only V8 isolates
- 1-Click Upgrade Portal: https://topaigateway.com/#pricing
