Convert JSON5 to JSON
What converting JSON5 to strict JSON changes, which values and comments are lost, and when the conversion is safe for configs, APIs and shared files.
Short answer: when converting JSON5 to JSON makes sense
Convert JSON5 to JSON when the destination requires standard JSON. Examples include an API payload, a parser that accepts only strict JSON, or a toolchain with no JSON5 support. The main benefit is compatibility: any conforming JSON parser can read the result. The main tradeoff is that comments and authoring conveniences are discarded, and a few JSON5 values have no JSON equivalent at all.
The safest pattern is to keep the JSON5 file as the source that people edit and to generate JSON from it as a build or export step. Do not replace the original. If the file is maintained by people and read only by software that already accepts JSON5, converting it adds a step and removes the comments for no gain. In that case, keep the JSON5 text as it is.
What the conversion actually changes
JSON5 is a superset of JSON. Every valid JSON document is also valid JSON5, but the reverse is not true. Converting JSON5 to JSON is therefore a syntax rewrite. A converter parses the JSON5 into data and then serializes that data as strict JSON. The stored characters change, unlike visual effects such as styled fonts or mirrored text. In most cases, the underlying data stays the same.
| JSON5 feature | What appears in the JSON output |
| Line comments (//) and block comments (/* */) | Removed entirely |
| Unquoted object keys, such as name: | Double-quoted keys, such as "name": |
| Single-quoted strings | Double-quoted strings, with inner characters escaped as needed |
| Trailing commas in objects and arrays | Removed |
| Hexadecimal numbers, such as 0x1F | Decimal numbers, such as 31 |
| Leading or trailing decimal points, such as .5 or 5. | Standard forms, such as 0.5 or 5 |
| Explicit plus sign, such as +1 | Unsigned number, such as 1 |
| Strings continued across lines with a backslash | A single-line string; the escaped line break is not part of the value |
| Infinity, -Infinity and NaN | No JSON equivalent; the result depends on the tool (see below) |
| Character escapes, such as \x41 | The represented character, such as A, or a JSON-compatible escape |
| Extra whitespace and layout | Replaced by the serializer's own indentation settings |
Worked example
Input in JSON5:
// app settings
{
name: 'demo',
port: 0x1F90,
ratio: .5,
tags: ['a', 'b',],
}
Output as JSON, using two-space indentation:
{
"name": "demo",
"port": 8080,
"ratio": 0.5,
"tags": [
"a",
"b"
]
}
Each difference matches the rules in the table:
• The comment is gone.
• The keys are double-quoted.
• The single quotes became double quotes.
• The hexadecimal 0x1F90 became its decimal value, 8080.
• The .5 became 0.5.
• Both trailing commas were dropped.
The array now spans several lines because of the serializer's indentation setting, not the source layout. Other tools may format whitespace differently.
Where meaning can shift
Most JSON5 files convert without any change in meaning. The cases below are the exceptions to check for before you rely on the output.
- Infinity, -Infinity and NaN: JavaScript's JSON.stringify writes these as null. That silently turns a number into an empty value. Some other converters reject the input instead. If your file uses these values, decide how they should be represented before you convert.
- Large integers: parsers that store numbers as double-precision floating point cannot represent every integer above 9007199254740992 (2 to the 53rd power). A large ID, especially one written in hexadecimal, can change without any warning.
- Duplicate keys: parse-and-reserialize tools generally keep only one value for a repeated key, often the last one. The earlier value disappears from the output with no error.
- Key order: JavaScript-based converters move integer-like keys, such as "1" or "2", to the front in ascending numeric order. The JSON standard does not treat object member order as meaningful, but the change can confuse diffs and human readers.
- Comments: any explanation, warning or instruction written in a comment is lost. If comments document why a value is set, keep the JSON5 source or move that documentation elsewhere.
Will the output stay readable and usable after copying or sharing?
The output is ordinary text. It uses no lookalike Unicode characters, reversed character order or display tricks. A faithful copy and paste therefore preserves it exactly, and search tools and screen readers handle it like any other code. The reading-order and assistive-technology problems that affect styled or mirrored text do not apply here.
Human readability depends on formatting. Minified single-line JSON is hard to scan, so choose an indentation setting if people will read the result. Even well-indented JSON is less self-explanatory than the JSON5 source because the comments are gone.
The main practical risk comes from where you paste the result. Some word processors and messaging apps convert straight quotation marks to curly ones, and curly quotes make the JSON invalid. Share JSON as a file or inside a code block, and run the received copy through a strict JSON parser if accuracy matters.
Choosing a conversion method
Converting locally with a JSON5 library gives you control over edge cases and keeps the data on your own machine. In Node.js with the json5 package, a minimal conversion looks like this:
const fs = require('fs');
const JSON5 = require('json5');
const source = fs.readFileSync('config.json5', 'utf8');
const data = JSON5.parse(source);
fs.writeFileSync('config.json', JSON.stringify(data, null, 2) + '\n');
This script inherits the JavaScript behaviors described above. NaN and Infinity become null, very large integers can lose precision, and integer-like keys are reordered. Add your own checks if your data contains any of these.
Online converters can be convenient for small, non-sensitive snippets. A tool's appearance or marketing language tells you nothing reliable about how it stores or logs what you paste. Avoid pasting credentials, tokens or private configuration into a tool unless its data handling is documented. Whichever method you use, validate the output with a strict JSON parser and test it against the edge cases that actually occur in your files.
Going the other way
Converting JSON to JSON5 needs no transformation, because valid JSON is already valid JSON5. Tools that offer this direction only restyle the file, for example by unquoting keys or switching to single quotes. They do not change the data.
Gaps in this review
This article relies on the JSON5 syntax rules and on general, documented parser behavior. It does not include hands-on tests of specific converter websites, editors, chat platforms or assistive technologies. Edge-case handling, especially for NaN, Infinity and large numbers, can differ between tools. Confirm the behavior of the tool you choose with your own sample data.