ToolXkit Icon

Tutorial · 7 min read · Updated 14 August 2026

How to clean, format, and validate malformed JSON data

Trailing commas, unquoted keys, single quotes, and escaped characters break parsers. Here is how to fix corrupt JSON fast.

JSON (JavaScript Object Notation, defined in RFC 8259) is what most web APIs, microservices, cloud config files and NoSQL databases use to pass data around. But raw JSON logs, scraped datasets and API payloads copied by hand often carry syntax errors, trailing commas, unescaped quote characters and broken encoding. Any of these will make a standard parser like JSON.parse() throw an error.

The Strict Grammar of JSON vs. JavaScript Object Literals

Many people assume JSON follows the loose rules of JavaScript. It does not. JSON is a strict format for exchanging data, and it has no room for guesswork:

  • Mandatory Double Quotes: In JavaScript, {name: 'ToolXkit'} is valid. In JSON, every key name and every string must sit inside plain ASCII double quotes ("key"). Single quotes (') or keys with no quotes at all cause a parse error straight away.
  • The Trailing Comma Trap: Modern JavaScript lets you leave a comma after the last array item or object property. JSON does not allow it. {"status": 200,} will fail in every parser that follows the RFC.
  • Numeric Literal Constraints: JSON does not allow hexadecimal numbers (0x1F), octal notation (0755), leading zeros (012) or a number that starts with a decimal point (.5 instead of 0.5).
  • Forbidden Primitive Values: JSON has no way to write undefined, NaN, Infinity or -Infinity. They have to be written as null or as a valid string.

Common JSON Syntax Faults and How to Fix Them

Broken / Invalid JSON Input Parser Error Message Clean & Valid JSON Correction
{'id': 101, 'active': true} Unexpected token ' at position 1 {"id": 101, "active": true}
{"items": ["apple", "orange",]} Unexpected token ] in JSON at position 29 {"items": ["apple", "orange"]}
{"notes": "Line 1 Line 2"} Bad control character in string literal {"notes": "Line 1\nLine 2"}
{"price": $49.99, "stock": NaN} Unexpected token $ in JSON {"price": 49.99, "stock": null}

Step-by-Step Guide to Formatting and Validating JSON Data

  1. Automated Lexical Parsing: Paste your raw JSON into the JSON formatter, which runs in your browser. It reads the text one character at a time and points out unclosed brackets, missing colons and stray commas or other separators.
  2. Recursive Key & Value Normalization: The formatter turns single quotes into the double quotes JSON needs, removes illegal trailing commas and tidies up escaped backslashes.
  3. Structured Indentation (Pretty-Printing): Indenting each level by 2 or 4 spaces makes deeply nested arrays and objects easy to scan, for developers and QA engineers alike.
  4. Schema Validation (Optional): Run the result through the JSON validator to check it against a target JSON Schema. This confirms that every required key is present and every type (string, boolean, integer) matches what the API expects.

Data Security: Why In-Browser JSON Formatting Is Important

Production API payloads and application logs often hold sensitive things: database connection strings, bearer tokens, customers' personally identifiable information (PII) and details of how your internal systems are built.

Many online formatters upload what you paste to their own servers. There it can be kept in access logs, collected for analytics or intercepted on the way. Our formatter runs 100% locally in your browser memory with JavaScript. It makes no network requests, so private code and credentials stay on your machine. If you need the opposite, the smallest possible payload instead of the most readable one, use the JSON minifier.

Frequently Asked Questions (FAQ)

Why does JSON require double quotes instead of single quotes?

Douglas Crockford designed JSON to be small and easy to use from any programming language (C++, Java, Python, Ruby, Go). Picking one string delimiter, the double quote, removed any doubt about how strings should be parsed.

How do I handle very large JSON files (100MB+)?

For files of many megabytes, use a streaming parser or line-delimited JSON (NDJSON / JSON Lines). That way you avoid loading one giant object into memory at once.

Tools mentioned in this article

Keep reading