JSON6 Practical Code Examples

Working JSON6 documents and the code that reads them. Every output below was produced by json-6 1.1.7.

TL;DR: JSON6 reads like a JavaScript object literal. Install json-6, call JSON6.parse(), and use the extra syntax where it helps readability. Use the examples here as starting points and the cheat sheet as a quick reference.

The examples move from whole documents to individual features, then to the API calls you use to read and write JSON6. Where an example shows a result, the value was obtained by running it, not inferred. Each section is self-contained, so you can jump straight to the one that matches your task.

A complete configuration file

This file uses most of the format in one place. Comments explain choices, numbers use the base that suits them, and a back-tick string holds a message that spans two lines.

// app.json6 - production settings { name: 'billing-service', listen: { host: '0.0.0.0', port: 0x1F90, // 8080 }, limits: { maxBodyBytes: 16_777_216, // 16 MiB timeoutMs: 30_000, }, fileMode: 0o640, // owner rw, group r features: 0b0000_0101, // bit 0 and bit 2 enabled banner: `Billing service Do not edit while the nightly job runs.`, 'x-request-id': true, // quotes optional here; hyphens are allowed in bare keys rollout: ['eu', 'us', 'apac',], }

Parsed, listen.port is 8080, maxBodyBytes is 16777216, fileMode is 416 (decimal for octal 640) and features is 5. The banner value contains a real line break between its two sentences.

Number formats

JSON6 accepts every form below. Underscores are ignored when the number is evaluated, so you can place them wherever grouping helps the eye.

{ decimal: 1_000_000, // 1000000 fraction: 1_0.5_0, // 10.5 hex: 0xFF_FF, // 65535 binary: 0b11, // 3 octal: 0o17, // 15 padded: [001, 002, 010], // [1, 2, 10] - decimal, not octal leadingDot: -.5, // -0.5 exp: 1e3, // 1000 infinite: -Infinity, missing: NaN, }

Leading zeros are decimal. 0123 is 123, not octal 83. If you want octal, write 0o123. Read the full rationale in the numbers reference.

String formats

Three quote characters are accepted, and every one of them may contain raw line breaks. A backslash at the end of a line removes the break, which is the JSON5 behavior you may already know.

{ double: "it's fine", single: 'say "hello"', tick: `both ' and " here`, kept: 'two lines', // 'two\nlines' joined: 'one \ line', // 'one line' emoji: '\u{1F600}', // code point escape byte: '\x41', // 'A' notATemplate: `total: ${amount}`, // literal text, no substitution }

An unrecognized escape such as \q is emitted without its backslash, so 'x\q' parses as xq. That is lenient by design; if you prefer an error, turn on esStrictCompatible for legacy octal escapes, and validate other escapes in review.

Keys and arrays

{ plain: 1, if: 2, // reserved words are fine as keys 'quoted key': 3, `tick key`: 4, 42: 5, // becomes the string key '42' list: [1, 2, 3,], holes: [1,, 3], // index 1 is empty nothing: undefined, }

Holes are real holes: [1,,3] has length 3 and no value at index 1, as in JavaScript. A doubled comma between object members is an error: {a:1,,b:2} throws.

Parsing and stringifying in Node.js

const JSON6 = require('json-6'); const config = JSON6.parse("{ port: 0x1F90, tags: ['a', 'b',], }"); // { port: 8080, tags: [ 'a', 'b' ] } JSON6.stringify({ port: 8080, name: 'svc', tags: ['a', 'b'] }); // {name:"svc",port:8080,tags:["a","b"]}

Two behaviors of stringify are worth knowing. Keys are sorted alphabetically by default, and keys that are valid identifiers are written without quotes. Pass { sortKeys: false } as the fourth argument to keep your own order. Output is JSON6, not JSON, so do not hand it to a JSON-only consumer; use JSON.stringify for that.

JSON6.stringify({ b: 1, a: 2 }, null, 2); // { // a: 2, // b: 1 // } JSON6.stringify({ b: 1, a: 2 }, null, null, { sortKeys: false }); // {b:1,a:2}

Using a reviver

const doc = JSON6.parse("{ when: '2026-10-27T00:00:00Z' }", (key, value) => key === 'when' ? new Date(value) : value); // { when: 2026-10-27T00:00:00.000Z }

Strict options

JSON6.parse("{ n: 0123 }"); // { n: 123 } JSON6.parse("{ n: 0123 }", null, { esStrictCompatible: true }); // throws JSON6.parse("`a${b}`", null, { forbidTemplateSubstitution: true }); // throws JSON6.parse("{ 1e3: true }", null, { normalizeNumericKeys: true }); // { '1000': true }

Streaming, require and the command line

JSON6.begin() builds a parser that accepts text in arbitrary chunks and calls your callback once for each complete top-level value. Values may be separated by whitespace, so a log of back-to-back objects works.

const seen = []; const parser = JSON6.begin(value => seen.push(value)); parser.write('{id:1}\n{id:'); parser.write('2}\n[3,4]'); // seen is [ {id:1}, {id:2}, [3,4] ]

To load .json6 files with require(), register the hook once, then require by path. The command line tool converts files to JSON. The README calls the executable json6, but npm installs it under the package name, so with json-6 installed the command is json-6.

require('json-6/lib/require'); const settings = require('./app.json6'); # convert app.json6 to app.json (4-space indent) npx json-6 -c app.json6

More on each of these is in the Node.js guide, the streaming guide and the CLI reference.

What an error looks like

Since version 1.1.6 the reported line and column are accurate, and malformed input that older versions silently accepted now throws. A parse failure includes the offending character, its offset, some nearby text and a [line:column] pair:

JSON6.parse('{a:@}'); // fault parsing '@' unexpected at 4 (near '{a:[@]}') [1:4] JSON6.parse('[1 2]'); // Two values with no separator between them ...

Browse parse errors for the common messages and their causes.

FAQ

Can I paste these examples into the validator?

Yes. The online validator parses what you paste with the JSON6 grammar and shows the resulting value.

Why does my file with comments fail in other tools?

Other tools expect plain JSON. Convert with json-6 -c at build time or load the file through json-6.

Does parse() reject trailing text after the value?

Not in 1.1.7: JSON6.parse('{a:1} junk') returns { a: 1 }. Check the input yourself if that matters.

Write Your First JSON6 File

Start with the getting started tutorial and have a parsed file in five minutes.