JSON5: Relaxed JSON with Comments and Trailing Commas

August 1, 20267 min read

Standard JSON was designed for machine-to-machine communication. Its strictness — no comments, no trailing commas, every key must be quoted — is a deliberate trade-off that simplifies parser implementation. But for developer-facing configuration files that humans read and edit daily, these restrictions are genuinely frustrating.

JSON5 is the response: a superset of JSON that adds the human-friendly features JSON lacks, while remaining valid JavaScript and easy to parse.

The Problem with Standard JSON for Config Files

Consider a typical application config file. With standard JSON:

{
  "server": {
    "host": "localhost",
    "port": 8080
  },
  "database": {
    "url": "postgres://localhost:5432/myapp",
    "pool": 10
  },
  "features": {
    "newDashboard": false,
    "betaApi": false
  }
}

You cannot explain why betaApi is false. You cannot add a TODO. You cannot leave a trailing comma after the last property to make adding new properties easier in future. JSON5 removes all these restrictions.

JSON5 Features

Comments

{
  // Single-line comment
  "host": "localhost",

  /* Multi-line comment
     explaining the port choice */
  "port": 8080,
}

Trailing Commas

{
  "name": "MyApp",
  "version": "2.0",   // trailing comma is valid
}

Unquoted Keys

{
  host: "localhost",   // no quotes needed for simple identifiers
  port: 8080,
}

Single-Quoted Strings

{
  "message": 'Hello, world!',
  "path": '/usr/local/bin',
}

Multi-Line Strings

{
  "query": "SELECT *
             FROM users
             WHERE active = true",
}

Hexadecimal Numbers

{
  "flags": 0xFF,
  "mask": 0x0F0F,
}

Leading and Trailing Decimal Points

{
  "low": .5,     // 0.5
  "high": 1.,    // 1.0
}

Infinity and NaN

{
  "max": Infinity,
  "invalid": NaN,
}

JSON5 vs JSON vs JSONC vs JSON with Comments

FormatCommentsTrailing commasUnquoted keysSingle quotesUsed by
JSON (RFC 8259)NoNoNoNoAPIs, data exchange
JSON5Yes (// /**/)YesYesYesBabel, some configs
JSONCYes (// /**/)YesNoNoVS Code settings

Where JSON5 is Used

Parsing JSON5 in JavaScript

import JSON5 from 'json5'

const config = JSON5.parse(`{
  // Server settings
  host: 'localhost',
  port: 8080,
  debug: false,  // trailing comma
}`)

console.log(config.host) // 'localhost'

// Stringify to JSON5
const json5str = JSON5.stringify(config, null, 2)
// or to standard JSON
const jsonstr = JSON.stringify(config, null, 2)

Install with: npm install json5

Parsing JSON5 in Python

import json5

config = json5.loads("""
{
  // App config
  host: 'localhost',
  port: 8080,
  features: {
    newDashboard: true,  // enable for beta users
  },
}
""")

print(config['host'])  # 'localhost'

Install with: pip install json5

Try JSON5 in IRJSON — Free Online

IRJSON's viewer supports a lenient parse mode that accepts JSON5-style input, including trailing commas, single-quoted strings and comments. Paste your JSON5 into the viewer and it will parse and format it correctly, making it easy to convert JSON5 config files to strict JSON when needed.

JSON5 vs YAML for Config Files

Both JSON5 and YAML solve the "JSON is too strict for config files" problem, but they make different trade-offs:

Frequently Asked Questions

Is JSON5 a standard?

JSON5 is not an IETF or ISO standard. It is a community specification maintained at json5.org. The json5 npm package is the reference implementation. It is widely used in developer tooling but should not be used for API data exchange where the client expects standard JSON.

Does Node.js support JSON5 natively?

No. require() and JSON.parse() both expect standard JSON. To use JSON5, install the json5 npm package and use JSON5.parse() instead of JSON.parse().

Can I use JSON5 in package.json?

No. npm requires standard JSON in package.json. If you want comments in your npm config, use // comments workaround keys that you document in a separate file, or store annotated configuration in a different file and generate package.json from it.

Parse and format JSON5 online

IRJSON's viewer handles JSON5 with trailing commas, single quotes and comments. Paste your JSON5 to explore or convert it to standard JSON.

Open IRJSON Viewer →