JSON5 Data Types

Complete reference for all JSON5 data types, their semantics, and usage patterns.

TL;DR: JSON5 supports six data types: objects, arrays, strings, numbers, booleans, and null. These map directly to JavaScript types, with JSON5 adding extended syntax for strings and numbers.

Type Overview

JSON5 has the same six data types as JSON, with extended syntax for strings and numbers. All types map directly to JavaScript equivalents when parsed.

JSON5 Type JavaScript Type Description JSON5 Extensions
Object object Key-value pairs Unquoted keys, trailing commas
Array Array Ordered list Trailing commas
String string Text data Single quotes, multi-line, hex escapes
Number number Numeric data Hex, Infinity, NaN, flexible decimals
Boolean boolean True or false None
Null null No value None

Type Hierarchy

JSON5Value ├── JSON5Object { ... } ├── JSON5Array [ ... ] ├── JSON5String "..." or '...' ├── JSON5Number 42, 0xFF, Infinity, NaN ├── JSON5Boolean true, false └── JSON5Null null

Objects

Objects are unordered collections of name/value pairs. They are the primary structure for representing complex data in JSON5.

Characteristics

Structure

  • Enclosed in curly braces {}
  • Zero or more key-value pairs
  • Pairs separated by commas
  • Keys and values separated by colon

Key Rules

  • Can be unquoted identifiers
  • Can be single or double quoted
  • Duplicate keys allowed (last wins)
  • Order is not guaranteed

Examples

// Empty object {} // Simple object with unquoted keys { name: 'John', age: 30, active: true, } // Nested objects { user: { profile: { name: 'Alice', email: '[email protected]', }, }, } // Mixed key styles { unquoted: 1, 'single-quoted': 2, "double-quoted": 3, }

JavaScript Mapping

JSON5 objects parse to JavaScript plain objects:

import JSON5 from 'json5'; const obj = JSON5.parse(`{ name: 'John', age: 30, }`); console.log(typeof obj); // 'object' console.log(obj.name); // 'John' console.log(Object.keys(obj)); // ['name', 'age']

Arrays

Arrays are ordered sequences of values. They maintain insertion order and can contain values of any type, including mixed types.

Characteristics

Structure

  • Enclosed in square brackets []
  • Zero or more values
  • Values separated by commas
  • Order is preserved

Value Rules

  • Any JSON5 value type allowed
  • Mixed types allowed
  • Nested arrays allowed
  • Trailing comma allowed

Examples

// Empty array [] // Simple array [1, 2, 3, 4, 5] // Array with trailing comma [ 'apple', 'banana', 'cherry', ] // Mixed types [ 'string', 42, true, null, { nested: 'object' }, ['nested', 'array'], ] // Nested arrays [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ]

JavaScript Mapping

const arr = JSON5.parse(`[1, 2, 3,]`); console.log(Array.isArray(arr)); // true console.log(arr.length); // 3 console.log(arr[0]); // 1

Strings

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

Quote Styles

Double Quotes (JSON compatible)

"Hello, World!" "Contains 'single' quotes" "Escape \"doubles\""

Single Quotes (JSON5)

'Hello, World!' 'Contains "double" quotes' 'Escape \'singles\''

Multi-line Strings

Strings can span multiple lines by escaping the line terminator:

{ poem: 'Roses are red, \ Violets are blue, \ JSON5 is awesome, \ And so are you!', } // Parses to: "Roses are red, Violets are blue, JSON5 is awesome, And so are you!"

Escape Sequences

Standard Escapes

"Tab:\tNewline:\n" "Carriage return:\r" "Backslash: \\" "Quote: \"" 'Apostrophe: \''

Unicode Escapes

// Unicode escape "\u0041" // "A" "\u4e2d\u6587" // "中文" // Hex escape (JSON5) "\x41" // "A"

String Semantics

  • Strings are immutable sequences of UTF-16 code units
  • Empty strings ("" or '') are valid
  • Surrogate pairs represent characters outside the BMP
  • Line terminators must be escaped (except with \ continuation)

JavaScript Mapping

const str = JSON5.parse(`'Hello, World!'`); console.log(typeof str); // 'string' console.log(str.length); // 13 console.log(str[0]); // 'H'

Numbers

Numbers in JSON5 follow IEEE 754 double-precision floating-point format. JSON5 extends JSON numbers with hexadecimal notation, special values, and flexible decimal syntax.

Number Formats

Decimal (JSON compatible)

42 // Integer -17 // Negative 3.14159 // Decimal 2.5e10 // Exponential 1e-5 // Negative exponent

Extended (JSON5 only)

0xFF // Hexadecimal .5 // Leading decimal 5. // Trailing decimal +10 // Explicit positive Infinity // Positive infinity NaN // Not a Number

Special Values

Infinity

Represents positive infinity. Can be prefixed with - for negative infinity or + for explicit positive.

Infinity // Positive infinity -Infinity // Negative infinity +Infinity // Explicit positive

NaN (Not a Number)

Represents an undefined or unrepresentable numeric value.

NaN // Not a Number

JSON Serialization: Infinity and NaN cannot be represented in standard JSON. When stringifying with JSON.stringify(), they become null.

Precision Limits

JSON5 numbers follow IEEE 754 double-precision limits:

Property Value JavaScript Constant
Max safe integer 9,007,199,254,740,991 Number.MAX_SAFE_INTEGER
Min safe integer -9,007,199,254,740,991 Number.MIN_SAFE_INTEGER
Max value ~1.8 x 10308 Number.MAX_VALUE
Min positive value ~5 x 10-324 Number.MIN_VALUE

JavaScript Mapping

const num = JSON5.parse(`0xFF`); console.log(num); // 255 console.log(typeof num); // 'number' const inf = JSON5.parse(`Infinity`); console.log(inf); // Infinity console.log(inf === Infinity); // true const nan = JSON5.parse(`NaN`); console.log(Number.isNaN(nan)); // true

Booleans

Booleans represent logical true/false values. JSON5 booleans are identical to JSON booleans with no extensions.

Boolean Values

{ enabled: true, disabled: false, }

Characteristics

  • Only two literal values: true and false
  • Case-sensitive (must be lowercase)
  • Not quoted (they are literals, not strings)

Note: True, TRUE, "true", 1 are NOT valid boolean values. Only lowercase true and false are accepted.

JavaScript Mapping

const bool = JSON5.parse(`true`); console.log(bool); // true console.log(typeof bool); // 'boolean' console.log(bool === true); // true

Null

Null represents the intentional absence of any value. JSON5 null is identical to JSON null with no extensions.

Null Literal

{ optional: null, missing: null, }

Characteristics

  • Only one literal value: null
  • Case-sensitive (must be lowercase)
  • Distinct from undefined (which doesn't exist in JSON5)

Common Use Cases

{ // Explicitly unset value middleName: null, // Placeholder for future value metadata: null, // Cleared/deleted value previousOwner: null, }

JavaScript Mapping

const value = JSON5.parse(`null`); console.log(value); // null console.log(value === null); // true console.log(typeof value); // 'object' (JavaScript quirk)

Ready to Start Using JSON5?

Check out our getting started guide or explore real-world examples.