Skip to content
Back to Blog
Tutorials

From JSON5 to JSONC: A More Forgiving JSON Formatting Guide

Explore JSON5 and JSONC syntax differences, formatting tools, and best practices for configs, CI/CD, and team collaboration with practical code examples.

Go Tools Team 7 min read

From JSON5 to JSONC: Unlocking a More Forgiving JSON Formatting Experience

When strict JSON makes you debug into the night because of one missing comma, it might be time to meet its more forgiving cousins — JSON5 and JSON with Comments (JSONC).

Why We Need “Lenient” JSON

  • Readability bottlenecks: Standard JSON forbids comments, trailing commas, single quotes, etc., which hurts clarity in configs and demos.
  • Team collaboration pain: Massive JSON files with zero inline notes are hard to maintain; a minor edit can break the whole file.
  • DevOps realities: Kubernetes and CI/CD pipelines frequently merge or generate JSON on the fly; forgiving syntax slashes manual fixes.

JSON5 vs. JSONC at a Glance

FeatureJSON (strict)JSON5JSONC
// single-line commentsNoYesYes
/* */ multi-line commentsNoYesYes
Trailing commasNoYesYes
Single-quoted stringsNoYesNo
Unquoted keysNoYesNo
Numeric signs + / -NoYesNo
Hex / binary numbersNoYesNo
Spec maintainersECMACommunity (A. Rauschmayer)Microsoft / VS Code

In one sentence: JSON5 feels like a JavaScript object literal, whereas JSONC is just standard JSON plus comments.

Syntax Examples

Vanilla JSON (strict)

{
  "name": "Go Tools",
  "features": ["Base64", "JSON Formatter"]
}

JSON5 Example

// Project config
{
  name: 'Go Tools',      // single quotes & unquoted keys
  features: [
    'Base64',
    'JSON Formatter',    // trailing comma
  ],
  version: 1.0,          // non-string number allowed
}

JSONC Example

{
  // Project name
  "name": "Go Tools",
  // Feature list
  "features": [
    "Base64",
    "JSON Formatter", // trailing comma
  ]
}

Formatting & Parsing Support

ScenarioJSON5JSONCRecommended libs / tools
BrowserJSON5.parse() polyfill, Prettier with built-in json5 parser (>= v1.13)VS Code settings.json native support
Node.jsjson5 npm packagecomment-json, jsonc-parserSupport CLI formatting & AST parsing
IDEVS Code, WebStorm extensionsVS Code native, WebStorm pluginPrettier can unify formatting
CI / Linteslint-plugin-json5eslint-plugin-jsoncIntegrate with Husky & lint-staged
Online toolGo Tools JSON Formatter (beautifies & validates JSON5/JSONC)SameJSON Formatter

Quick VS Code Formatting

Add to settings.json:

"[jsonc]": {
  "editor.defaultFormatter": "esbenp.prettier-vscode"
}

Then hit Alt + Shift + F to beautify any JSONC file.

Practical Tips & Caveats

1. Convert before production

  • Browsers still require strict JSON for JSON.parse.
  • Use rollup-plugin-json5 or custom scripts in the build step to transpile JSON5/JSONC to pure JSON.

2. Security & reliability

  • Leniency can hide rogue fields; validate with JSON Schema or zod before shipping APIs.

3. Team conventions

  • Declare in README that *.json5 is for configs only; release packages should ship strict .json.
  • Standardize Prettier rules to avoid noisy diffs.

4. Performance

  • Parsing JSON5/JSONC adds dependencies and slight overhead — fine for CLI & dev tooling, but measure for prod.

Wrap-Up

Balancing readability and rigor is a classic dev struggle. JSON5 and JSONC improve the authoring experience, and with the right build & validation pipeline, you can enjoy the perks of “lenient JSON” safely.

References

Related Articles

View all articles