JavaScript

JSON Repair: Fix Malformed JSON Automatically

JSON Repair is a JavaScript library that repairs malformed JSON by fixing common errors like missing quotes, trailing commas, and unescaped characters.

Keeping this site alive takes effort — your support means everything.
無程式碼也能輕鬆打造專業LINE官方帳號!一鍵導入模板,讓AI助你行銷加分! 無程式碼也能輕鬆打造專業LINE官方帳號!一鍵導入模板,讓AI助你行銷加分!
JSON Repair: Fix Malformed JSON Automatically

Few things are as frustrating as receiving malformed JSON from an API, a configuration file, or a data export. The error messages are often cryptic, and manually fixing the JSON in a large file is tedious and error-prone. JSON Repair (josdejong/jsonrepair on GitHub) solves this problem by providing a JavaScript library that automatically detects and fixes common JSON formatting errors.

Created by Jos de Jong – the same developer behind math.js – JSON Repair has become an essential utility in the JavaScript ecosystem. It handles a comprehensive range of JSON errors: missing quotes around keys, missing quotes around string values, trailing commas in objects and arrays, missing commas between items, single quotes instead of double quotes, unescaped newlines and tabs, truncated JSON, and even concatenated JSON strings.

The library’s repair algorithm works character by character through the JSON string, applying heuristic rules based on context. This approach allows it to handle multiple simultaneous errors in a single pass, producing valid JSON output even from significantly malformed input. When the damage is too severe for automatic repair, the library provides clear error messages indicating exactly where and why repair failed.


Error Detection and Repair Flow

JSON Repair’s processing pipeline combines parsing with automatic error correction:

This approach enables JSON Repair to handle complex cases with multiple interacting errors, such as a truncated JSON object that is also missing commas and has unescaped characters.


Supported Error Types and Repair Methods

Error TypeExample InputRepair Action
Missing key quotes{name: “value”}Add quotes: {“name”: “value”}
Missing string quotes{“name”: value}Add quotes: {“name”: “value”}
Trailing comma[1, 2,]Remove comma: [1, 2]
Missing comma{“a”:1 “b”:2}Add comma: {“a”:1, “b”:2}
Single quotes{‘key’: ‘value’}Convert to double: {“key”: “value”}
Unescaped newlines“line1\nline2”Escape: “line1\nline2”
Truncated JSON{“a”:1, “b”:Close gracefully: {“a”:1, “b”: null}
Concatenated JSON{“a”:1}{“b”:2}Wrap in array: [{“a”:1}, {“b”:2}]
Comment stripping// comment\n{“a”:1}Remove comments: {“a”:1}

Real-World Applications

JSON Repair finds practical use in several common scenarios. Developers building web scrapers use it to clean up JSON data embedded in HTML pages that is often malformed due to poor templating. API integration tools apply it as a fallback when receiving responses from poorly documented or non-compliant endpoints. Configuration management systems use it to parse user-provided configuration files that might contain errors.

The library is also useful during development and debugging. When an application produces broken JSON, piping it through JSON Repair can often salvage enough data to diagnose the root cause of the problem. Its clear error reporting identifies exactly which parts of the JSON structure are problematic and why.



FAQ

What is JSON Repair? JSON Repair is a JavaScript library created by Jos de Jong that automatically repairs malformed JSON strings. It detects and fixes common JSON errors including missing quotes around keys or string values, trailing or missing commas, unescaped control characters, single quotes used instead of double quotes, and truncated JSON documents.

What types of JSON errors can JSON Repair fix? JSON Repair can fix a wide range of errors: missing quotes around keys, missing quotes around string values, trailing commas in objects and arrays, missing commas between items, single quotes instead of double quotes, unescaped newlines and tabs in strings, truncated JSON, concatenated JSON strings, and even some JavaScript object literal syntax that is not valid JSON.

How does JSON Repair’s error correction algorithm work? JSON Repair uses a character-by-character parsing approach with error recovery. When it encounters an invalid JSON construct, it applies a series of heuristic repair rules based on the context. For example, if it finds a single quote where a double quote is expected, it automatically converts it. The library handles multiple simultaneous errors in a single pass.

Can JSON Repair be used in Node.js and browser environments? Yes, JSON Repair works in both Node.js and browser environments. It has zero dependencies and can be installed via npm for Node.js projects or loaded directly as a script in the browser. The library is lightweight, typically under 10KB minified and gzipped.

Is JSON Repair safe to use with untrusted input? JSON Repair is designed with safety in mind and does not eval or execute any code. It parses and repairs the JSON structure using its own parsing logic without relying on JavaScript’s eval function. This makes it safe to use with untrusted input, including data from third-party APIs, user uploads, or web scraping results.


Further Reading

TAG
CATEGORIES