JSONRepair

JSON5 vs JSON: What JSON5 Allows That Strict JSON Rejects

JSON5 vs JSON is a one-direction question. Every file that is valid strict JSON is also valid JSON5, but the reverse almost never holds. JSON5 keeps the JSON shape you know and drops the parts that punish humans: no comments, no trailing commas, no single quotes, no unquoted keys. If your parser chokes on a config that looks almost right, JSON5 is probably the name of what you are looking at.

I met JSON5 the way most people do. A language model handed me a config file with a trailing comma and a helpful // comment on line two, and JSON.parse died on line one. The file was not broken. It was written in a dialect the parser never learned. That dialect has a name, a published spec at spec.json5.org, and a growing pile of hand-edited config files written in it.

JSON5 started as a simple observation: JSON is a great wire format and a miserable writing format. Machines do not need comments. Humans annotating a 400-line config do. So JSON5 took the ECMAScript 5.1 object literal syntax, the thing every JavaScript developer already writes, and formalized it as a data format.

What JSON5 allows that strict JSON rejects

Seven relaxations cover nearly everything you will meet. Single-line and multi-line comments, so you can explain a value in place. Trailing commas in objects and arrays, so reordering lines never breaks the file. Unquoted object keys, as long as they are valid identifier names. Single-quoted strings, useful when the value itself contains double quotes. Multi-line strings through a backslash line continuation. Hexadecimal numbers like 0xdecaf. And number forms JSON forbids: a leading decimal point like .5, a trailing one like 5., an explicit plus sign, and the special values Infinity, -Infinity, and NaN.

The mental model that keeps me straight: JSON is a wire format, JSON5 is a writing format. On the wire, between machines, strictness is a feature. Every parser on earth agrees on RFC 8259, and that agreement is the whole point. In a config file a human edits by hand, strictness is a tax you pay every time you comment out a line or leave a comma after the last item.

The catch is tooling. No browser ships a JSON5 parser, and Node's JSON.parse does not read it. You need a library. The reference implementation is the json5 npm package, and most languages have an equivalent. So the rule for real projects: anything that crosses a system boundary, an API request, a message queue, a file another tool reads, ships as strict JSON. Parse the human-written file as JSON5, then JSON.stringify the result. That two-step round trip is the entire migration path, and it drops the comments and normalizes every relaxation on the way out.

One related format worth naming, because VS Code users meet it first: JSONC. JSONC is JSON plus comments (most parsers also tolerate trailing commas). JSON5 is the larger superset. They overlap, they are not interchangeable, and neither is valid input to JSON.parse. If a tool chokes on your file, check which dialect you actually wrote before you blame the tool.

My working setup is simple. I write configs in JSON5 where the tooling supports it, ship strict JSON on the wire, and keep the converter one command away. And when a pasted LLM answer or a hand-edited config fails strict parsing at midnight, do not hand-fix commas. Run it through a JSON5 parser and let the machine do the strict part.

Paste your almost-JSON config and get strict, valid JSON back, with every fix explained in plain English.

Fix My JSON Now

Frequently asked questions

Is JSON5 valid JSON?

No. Every valid strict JSON document is valid JSON5, but a JSON5 file that uses comments, trailing commas, or unquoted keys is not valid JSON. The compatibility runs in one direction only.

Can JSON.parse read JSON5?

No. JSON.parse implements strict JSON and rejects all of JSON5's relaxations. Use the json5 library, or a JSON5 parser for your language, to read JSON5, then JSON.stringify if you need strict JSON as output.

What is the difference between JSON5 and JSONC?

JSONC adds comments to JSON and is the format VS Code uses for settings files. JSON5 is a broader superset: comments, trailing commas, unquoted keys, single-quoted strings, hexadecimal numbers, and Infinity and NaN. A JSON5 file is not necessarily valid JSONC, and neither parses with JSON.parse.

Should I use JSON5 for API responses?

No. APIs are machine-to-machine, and every consumer expects strict JSON. JSON5 fits hand-edited configuration files. Convert to strict JSON at the system boundary.

How do I convert JSON5 to strict JSON?

Parse it with a JSON5 library and re-serialize with JSON.stringify. The round trip drops comments and normalizes every relaxation into strict JSON.

Related reading: JSON with Comments (JSONC): What It Is and How to Handle It · How to Fix the JSON Single Quotes Error (Python Dicts, LLM Output, Apostrophes) · The Trailing Comma That Breaks Your JSON (And the 30-Second Fix)

Get one practical JSON debugging note a week: subscribe on Substack.