JSON5 Syntax Reference

Complete syntax rules and production definitions for the JSON5 data format.

TL;DR: JSON5 syntax follows ECMAScript 5.1 conventions. Objects use curly braces, arrays use brackets, strings can be single or double quoted, and comments use JavaScript-style // or /* */ syntax.

JSON5 Values

A JSON5 document consists of a single JSON5 value, which can be any of the following types:

Object

Key-value pairs

Array

Ordered list

String

Text data

Number

Numeric data

Boolean

true or false

Null

No value

Value Production

JSON5Value: JSON5Null JSON5Boolean JSON5String JSON5Number JSON5Object JSON5Array

Objects

An object is an unordered collection of zero or more name/value pairs, enclosed in curly braces {}. JSON5 objects extend JSON objects with unquoted keys, single-quoted keys, and trailing commas.

Object Syntax

{ // Unquoted keys (valid ECMAScript identifiers) unquoted: 'value', // Single-quoted keys 'single-quoted': 'allows special chars', // Double-quoted keys (JSON compatible) "double-quoted": "standard JSON", // Trailing comma allowed last: 'value', }

Object Key Rules

Unquoted Keys

Keys can be unquoted if they are valid ECMAScript 5.1 identifiers:

  • Start with: letter, _, $, or Unicode escape
  • Followed by: letters, digits, _, $, or Unicode escapes
  • Cannot be reserved words: break, case, class, etc.

Quoted Keys

Keys can use single or double quotes. This allows any string as a key, including special characters, spaces, and reserved words.

Valid vs Invalid Examples

Valid

{ name: 'value', $price: 100, _private: true, 'with-dash': 'ok', '123start': 'ok', }

Invalid

{ with-dash: 'error', 123start: 'error', class: 'reserved', has spaces: 'error', }

Object Production

JSON5Object: { } { JSON5MemberList } { JSON5MemberList , } JSON5MemberList: JSON5Member JSON5MemberList , JSON5Member JSON5Member: JSON5MemberName : JSON5Value JSON5MemberName: JSON5Identifier JSON5String

Arrays

An array is an ordered sequence of zero or more values, enclosed in square brackets []. JSON5 arrays extend JSON arrays with trailing commas.

Array Syntax

[ 'first', 'second', 'third', // trailing comma allowed ]

Nested Structures

Arrays can contain any JSON5 values, including nested objects and arrays:

[ { name: 'Alice', scores: [95, 87, 92], }, { name: 'Bob', scores: [88, 91, 85], }, ]

Array Production

JSON5Array: [ ] [ JSON5ElementList ] [ JSON5ElementList , ] JSON5ElementList: JSON5Value JSON5ElementList , JSON5Value

Strings

Strings are sequences of Unicode characters enclosed in single or double quotes. JSON5 extends JSON strings with single quotes, additional escape sequences, and multi-line support.

Quote Styles

{ double: "double-quoted string", single: 'single-quoted string', // Single quotes make it easier to include double quotes html: '<div class="container">', // Double quotes make it easier to include single quotes text: "It's a nice day", }

Escape Sequences

JSON5 supports all JSON escape sequences plus additional ones from ECMAScript 5.1:

Escape Character Unicode Notes
\' Apostrophe U+0027 JSON5 only
\" Quotation mark U+0022 JSON & JSON5
\\ Backslash U+005C JSON & JSON5
\b Backspace U+0008 JSON & JSON5
\f Form feed U+000C JSON & JSON5
\n Line feed U+000A JSON & JSON5
\r Carriage return U+000D JSON & JSON5
\t Horizontal tab U+0009 JSON & JSON5
\v Vertical tab U+000B JSON5 only
\0 Null U+0000 JSON5 only
\xHH Hex escape U+00HH JSON5 only
\uHHHH Unicode escape U+HHHH JSON & JSON5

Multi-line Strings

Strings can span multiple lines by escaping the line terminator with a backslash:

{ multiline: 'This is line one \ and this is line two \ and this is line three', }

Note: The backslash and line terminator are not included in the string value. The above example produces: "This is line one and this is line two and this is line three"

String Production

JSON5String: " JSON5DoubleStringCharacter? " ' JSON5SingleStringCharacter? ' JSON5DoubleStringCharacter: SourceCharacter but not " or \ or LineTerminator \ EscapeSequence LineContinuation U+2028 U+2029 JSON5SingleStringCharacter: SourceCharacter but not ' or \ or LineTerminator \ EscapeSequence LineContinuation U+2028 U+2029

Numbers

JSON5 numbers extend JSON numbers with hexadecimal notation, leading/trailing decimal points, explicit positive signs, and IEEE 754 special values.

Number Formats

{ // Standard JSON numbers integer: 42, negative: -17, decimal: 3.14159, exponent: 1.5e10, negativeExponent: 2.5e-5, // JSON5 extensions hexadecimal: 0xFF, hexLower: 0xdead, hexUpper: 0xBEEF, leadingDecimal: .5, trailingDecimal: 5., positiveSign: +10, positiveExponent: 1e+10, // IEEE 754 special values infinity: Infinity, negativeInfinity: -Infinity, positiveInfinity: +Infinity, notANumber: NaN, }

Number Categories

Decimal Numbers

  • 42 - Integer
  • -17 - Negative integer
  • +10 - Explicit positive (JSON5)
  • 3.14 - Decimal
  • .5 - Leading decimal (JSON5)
  • 5. - Trailing decimal (JSON5)
  • 1e10 - Exponential

Hexadecimal Numbers

  • 0xFF - Uppercase hex
  • 0xff - Lowercase hex
  • 0xDeadBeef - Mixed case
  • -0xFF - Negative hex
  • +0xFF - Positive hex (JSON5)

Special Values (JSON5 only)

  • Infinity - Positive infinity
  • -Infinity - Negative infinity
  • NaN - Not a Number (represents undefined or unrepresentable values)

Important: Infinity and NaN cannot be serialized to standard JSON. If you use these values and later need JSON output, you'll need to handle them specially (e.g., convert to null or strings).

Number Production

JSON5Number: JSON5NumericLiteral Infinity NaN JSON5NumericLiteral: NumericLiteral NumericLiteral: DecimalLiteral HexIntegerLiteral DecimalLiteral: DecimalIntegerLiteral . DecimalDigits? ExponentPart? . DecimalDigits ExponentPart? DecimalIntegerLiteral ExponentPart? HexIntegerLiteral: 0x HexDigit+ 0X HexDigit+

Comments

JSON5 supports JavaScript-style comments. Comments are completely ignored during parsing and do not affect the resulting data structure.

Single-Line Comments

Single-line comments start with // and continue to the end of the line:

{ // This is a single-line comment name: 'app', version: '1.0', // Inline comment }

Multi-Line Comments

Multi-line comments are enclosed between /* and */:

{ /* * This is a multi-line comment. * It can span multiple lines. * Useful for longer explanations. */ database: { host: 'localhost', port: 5432, }, }

Comment Placement

Comments can appear anywhere whitespace is allowed:

// File header comment { /* Before key */ key /* After key */: /* Before value */ 'value' /* After value */, array: [ // Comment in array 1, /* Another comment */ 2, ], } // Trailing comment

Note: Comments are not preserved when parsing JSON5. If you parse a JSON5 file and stringify the result, comments will be lost. This is standard behavior for data interchange formats.

Comment Production

Comment: MultiLineComment SingleLineComment MultiLineComment: /* MultiLineCommentChars? */ SingleLineComment: // SingleLineCommentChars? MultiLineCommentChars: MultiLineNotAsteriskChar MultiLineCommentChars? * PostAsteriskCommentChars? SingleLineCommentChars: SingleLineCommentChar SingleLineCommentChars? SingleLineCommentChar: SourceCharacter but not LineTerminator

Whitespace

JSON5 recognizes all whitespace characters defined by ECMAScript 5.1, providing more flexibility than JSON's limited whitespace support.

Whitespace Characters

Character Unicode Name JSON JSON5
\t U+0009 Tab
\n U+000A Line Feed
\r U+000D Carriage Return
(space) U+0020 Space
\v U+000B Vertical Tab
\f U+000C Form Feed
U+00A0 No-Break Space
U+FEFF Byte Order Mark
U+2028 Line Separator
U+2029 Paragraph Separator

Whitespace Production

WhiteSpace: <TAB> <VT> <FF> <SP> <NBSP> <BOM> <USP> LineTerminator: <LF> <CR> <LS> <PS>

Ready to Use JSON5?

Check out our language-specific guides for implementation details.