Skip to main content
CalcChef

JSON Formatter

Paste JSON to format it with 2 spaces, 4 spaces or tabs, minify it or sort its keys. A syntax error is reported with its line, column and a caret under it. How it works

Use the JSON Formatter

Valid
Output
{
  "dealership": {
    "name": "AutoWorld",
    "established": 1995,
    "inventory": [
      {
        "id": "C001",
        "make": "Tesla",
        "model": "Model 3",
        "year": 2023,
        "features": {
          "autopilot": true,
          "battery": "Long Range"
        },
        "price": 45990
      },
      {
        "id": "C002",
        "make": "Ford",
        "model": "Mustang",
        "year": 1967,
        "isVintage": true,
        "specs": {
          "engine": "V8",
          "horsepower": 335
        },
        "price": 55000
      }
    ],
    "location": {
      "city": "Detroit",
      "zip": "48201"
    }
  }
}
Input: 368 charsOutput: 650 charsEst. size: 650 bytes (UTF-8)

About the JSON Formatter

JSON does not allow trailing commas, comments, single quotes or unquoted keys, so {"a": 1,} is a syntax error. This JSON formatter reports the first error with its line and column and a caret under the spot. Valid JSON is formatted with 2 spaces, 4 spaces or tabs, minified, or printed with its keys sorted.

Common JSON syntax errors and what the formatter reports

MistakeInputReported atMessage in Chrome and Edge
Trailing comma in an object{"a": 1,}Line 1, column 9Expected double-quoted property name
Trailing comma in an array[1, 2,]Line 1, column 7Unexpected token ']', "[1, 2,]" is not valid JSON
Single quotes{'a': 1}Line 1, column 2Expected property name or '}'
Unquoted key{a: 1}Line 1, column 2Expected property name or '}'
Comment{"a": 1, /* id */ "b": 2}Line 1, column 10Expected double-quoted property name
Missing comma{"a": 1 "b": 2}Line 1, column 9Expected ',' or '}' after property value
Missing colon{"a" 1}Line 1, column 6Expected ':' after property name
Leading zero{"a": 01}Line 1, column 8Unexpected number
NaN as a value{"a": NaN}Line 1, column 7Unexpected token 'N', "{"a": NaN}" is not valid JSON
Missing closing brace{"a": 1Line 1, column 8Expected ',' or '}' after property value

The line and column come from the formatter’s own scan of the text and do not depend on the browser. The message is the browser’s: Firefox and Safari word it differently.

One document after each option

OptionLinesCharacters
Minify158
Format, 2 spaces988
Format, 4 spaces9106
Format, tabs979

Input: { "name": "Ada", "tags": ["math", "code"], "active": true, "id": 7 }, which is 68 characters as typed.

How to use this tool

  1. Paste JSON into the Input JSON box, or press Open .json file to load a .json or .txt file of up to 5,000,000 bytes, which is formatted at once. A paste into an empty box, or over a full selection, is formatted at once; a paste into the middle of existing text is inserted as usual and the output is marked as out of date. Typed changes are checked after a short pause and marked Valid or Invalid.
  2. Choose the indent (2 spaces, 4 spaces or tabs) and tick Sort keys if you want every object’s keys sorted (by character code, so capitals before lowercase and number-like keys first), then press Format or Minify.
  3. Read the output as Raw text or as a Tree, search it, copy it, or download it as formatted.json or minified.json.

Frequently asked questions

Why does JSON give an error for a trailing comma?
The JSON grammar (RFC 8259) allows a comma only between two values, never after the last one. JavaScript object literals accept it; JSON does not. Delete the comma before the closing } or ]. The error points at the bracket after the comma, because that is where the parser expected another value.
Can JSON have comments?
No. RFC 8259 defines no comment syntax, so // and /* */ are reported as syntax errors. Remove the comment, or keep the note as an ordinary string value.
How do I minify JSON?
Paste it and press Minify. All whitespace between values is removed and nothing else changes. The sample document on the page is 650 characters formatted and 368 minified.
Why did a number change after formatting?
JavaScript numbers hold whole numbers exactly only up to 9,007,199,254,740,991 (2^53 − 1), and about 17 significant digits in all. 12345678901234567890 comes back as 12345678901234567000, and 1e400 is too large for a JavaScript number and becomes null. The formatter shows a warning with the numbers it happened to. To keep every digit of an ID, store it as a string.
What happens to duplicate keys?
Only the last value is kept, and the formatter warns you and names the duplicated keys with their lines. RFC 8259 says names in an object should be unique and notes that parsers handle duplicates differently.
Is my JSON uploaded?
No. Pasted JSON and opened files are parsed and formatted in your browser, in a background worker when the document is over 50,000 characters, and the tool makes no network request.

Related Tools

View all tools