Need help with your JSON?

Try our JSON Formatter tool to automatically identify and fix syntax errors in your JSON. JSON Formatter tool

JSON Formatter Cheat Sheets for Quick Reference

Need a fast way to check whether your JSON should format cleanly? This cheat sheet focuses on the rules that break formatters most often, shows quick invalid-to-valid fixes, and highlights the current JSON.stringify() behaviors that matter in real debugging.

Fast mental model

A JSON formatter can only pretty-print input that already parses as valid JSON. It will not automatically convert JavaScript object literals, comments, trailing commas, undefined, or BigInt into valid JSON for you.

One-Screen JSON Cheat Sheet

RuleValidInvalidWhy it matters
Top-level JSON can be any single JSON value"hello""hello" "world"Objects and arrays are common, but a JSON document can also be a string, number, boolean, or null.
Object keys must be double-quoted strings{ "name": "Ada" }{ name: "Ada" }Unquoted keys are valid in JavaScript object literals, not in JSON.
Strings use double quotes and escapes{ "line": "A\nB" }{ "line": 'A B' }Use escapes like \" \\ \n \t and \uXXXX when needed.
Numbers cannot use leading zeroes, NaN, or Infinity{ "count": 10, "ratio": 0.25 }{ "count": 01, "ratio": NaN }JSON numbers are decimal only. Hex, octal, Infinity, and NaN are not valid JSON values.
Commas only separate items, never trail{ "a": 1, "b": 2 }{ "a": 1, "b": 2, }A formatter cannot recover from trailing commas until the JSON parses successfully.
Comments are not part of JSON{ "enabled": true }{ "enabled": true // comment }If you need comments in a config file, use the tool's supported format such as JSONC, YAML, or TOML instead.
Whitespace is optional and ignored by parsers{"a":1}NonePretty-printed and minified JSON represent the same data if the token order and values are unchanged.
Duplicate keys are technically legal text but unsafe{ "id": 1, "name": "Ada" }{ "id": 1, "id": 2 }Different parsers may handle duplicate names differently. Avoid them if you want predictable results.

Copy-Paste Starter Snippets

Valid object template

{
  "id": 123,
  "name": "Ada Lovelace",
  "active": true,
  "tags": ["math", "programming"],
  "profile": {
    "city": "London"
  },
  "notes": null
}

Valid array template

[
  {
    "id": 1,
    "name": "Ada"
  },
  {
    "id": 2,
    "name": "Grace"
  }
]

Pretty-printed JSON

{
  "ok": true,
  "items": [
    1,
    2,
    3
  ]
}

Minified JSON

{"ok":true,"items":[1,2,3]}

Common Inputs That Break JSON Formatters

These are the cases that most often cause a formatter to fail immediately. Fix the syntax first, then format the result.

JavaScript object literal pasted into a JSON formatter

Invalid input

{ user: 'Ada', active: true }

Valid JSON

{ "user": "Ada", "active": true }

Trailing commas in objects or arrays

Invalid input

{ "tags": ["json", "formatting",], }

Valid JSON

{ "tags": ["json", "formatting"] }

Comments left in API payloads or config

Invalid input

{
  "timeout": 30 // seconds
}

Valid JSON

{
  "timeout": 30
}

Invalid numbers

Invalid input

{ "count": 01, "speed": Infinity }

Valid JSON

{ "count": 1, "speed": 999999 }

JavaScript-only values

Invalid input

{ "missing": undefined, "id": 9007199254740993n }

Valid JSON

{ "missing": null, "id": "9007199254740993" }

JSON vs. JavaScript object literals

Many formatter errors come from pasting JavaScript instead of JSON. JavaScript may allow unquoted keys, single quotes, trailing commas, comments, undefined, and 123n BigInt literals. JSON does not. If the source came from application code, convert it to real JSON before running it through a formatter.

Using JSON.stringify() for Formatting

In browsers and Node.js, JSON.stringify(value, replacer, space) is the fastest way to generate formatted JSON from an already-valid JavaScript value.

PatternWhat it doesExample
JSON.stringify(value)Minified JSON stringJSON.stringify({ name: "Ada", admin: false })
JSON.stringify(value, null, 2)Pretty-printed with 2 spacesJSON.stringify({ name: "Ada" }, null, 2)
JSON.stringify(value, ["id", "name"], 2)Keeps only listed object keysJSON.stringify(user, ["id", "name"], 2)
JSON.stringify(value, replacerFn, 2)Transforms or drops values during serializationJSON.stringify(data, (key, value) => (key === "secret" ? undefined : value), 2)

Pretty-print with two spaces

const data = {
  name: "Ada",
  languages: ["Analytical Engine notes", "Mathematics"]
};

const formatted = JSON.stringify(data, null, 2);
/*
{
  "name": "Ada",
  "languages": [
    "Analytical Engine notes",
    "Mathematics"
  ]
}
*/

Current JSON.stringify() gotchas

These behaviors are the ones developers most often forget when a formatter output looks different from the original JavaScript data.

  • undefined, functions, and symbols are dropped from objects

    JSON.stringify({ a: undefined, b: () => {}, c: Symbol("x") })

    `{}`. In arrays, those same values become `null` instead.

  • BigInt throws instead of serializing

    JSON.stringify({ id: 2n })

    Throws `TypeError` unless you convert the value yourself, typically to a string.

  • NaN and Infinity become null

    JSON.stringify({ score: NaN, limit: Infinity })

    `{"score":null,"limit":null}`.

  • Date uses its JSON representation

    JSON.stringify({ createdAt: new Date("2026-03-10T12:00:00Z") })

    Dates serialize as ISO strings such as `{"createdAt":"2026-03-10T12:00:00.000Z"}`.

  • Map and Set are not serialized the way many people expect

    JSON.stringify({ map: new Map([["x", 1]]), set: new Set([1, 2]) })

    Usually `{"map":{},"set":{}}` unless you convert them first.

  • Indentation is capped

    JSON.stringify(data, null, 20)

    The indentation width is capped at 10 spaces. String indentation is truncated to its first 10 characters.

  • Circular references still fail

    const obj: { self?: unknown } = {}; obj.self = obj; JSON.stringify(obj);

    Throws because plain JSON cannot represent a circular structure.

Troubleshooting Checklist

  • Start with the first parse error. Later errors are often side effects of the first broken token.
  • Replace single quotes with double quotes only where they are part of JSON syntax, not inside the data.
  • Remove comments and trailing commas before trying to format.
  • Check number literals for leading zeroes, missing digits after decimal points, and JavaScript-only values.
  • If you are serializing JavaScript, decide how to handle undefined, Date, Map, Set, NaN, Infinity, and BigInt before calling JSON.stringify().
  • If the data is newline-delimited JSON (JSON Lines / NDJSON), format each line as its own JSON document.

Use this page as a quick reference when a formatter rejects your input or when you need a reminder of how JavaScript values turn into JSON text. The shorter the feedback loop, the faster you can fix invalid payloads and move on.

Need help with your JSON?

Try our JSON Formatter tool to automatically identify and fix syntax errors in your JSON. JSON Formatter tool