Broken JSON Auto-Repair & Formatter Technical Architecture Guide
When foundation models output JSON responses, hitting max token limits or experiencing stream drops leads to unclosed brackets, causing fatal `SyntaxError: Unexpected end of JSON input` errors. SafeAI JSON Fixer reconstructs broken structures in client memory by dynamically tracking AST nesting states.
1. Stack-Based Finite State Machine (FSM) Bracket Balancer
SafeAI maintains an internal state stack tracking quote escapes, object open brackets (`{`), and array brackets (`[`). When truncated text is fed, the engine terminates open string literals, discards dangling commas, and appends the necessary closing braces in correct hierarchical order to generate valid JSON.
2. Common Truncated JSON Scenarios & Auto-Repairs
| Error Scenario | Truncated Input Snippet | SafeAI Reconstructed Output |
|---|---|---|
| Unclosed Object | {"user": "Admin", "id": 10 | {"user": "Admin", "id": 10} |
| Mid-String Cutoff | {"message": "Incomplete text | {"message": "Incomplete text"} |
| Unclosed Array | {"tags": ["ai", "security" | {"tags": ["ai", "security"]} |
| Trailing Commas | {"data": 123,} | {"data": 123} |
3. JSON Streaming & Pipeline Troubleshooting
-
✓
Trailing Comma Normalization: Standard ECMAScript JSON forbids trailing commas. SafeAI automatically purges them.
-
✓
Dangling Key Assignment: If a key exists without a value, SafeAI auto-assigns `null` to complete the token requirement.
-
✓
Formatted Beautification: Includes instant 2-space pretty printing for immediate inspection and copy.