JSON Formatter

Paste JSON to indent it, minify it, or find out why it will not parse. When the input is broken, the error names the line where parsing stopped, and the mistake sits on that line or just before it. Often it is a trailing comma or a single-quoted string.

Input JSON
Formatted / Minified JSON

1 JSON Formatter example

Input (Minified)

1
{"id":1,"name":"John Doe","roles":["admin","user"],"active":true,"meta":{"created":"2023-01-01","login_count":42}}

Output (Formatted)

1
2
3
4
5
6
7
8
9
10
11
12
13
{
  "id": 1,
  "name": "John Doe",
  "roles": [
    "admin",
    "user"
  ],
  "active": true,
  "meta": {
    "created": "2023-01-01",
    "login_count": 42
  }
}

2 What the JSON Formatter helps with

Finding the syntax error

Look just before the reported spot. The error names the line where parsing stopped, which is usually one token after the real mistake. For example, a trailing comma is reported at the closing brace that follows it.

Reading an API response

A minified response puts everything on one line. Indented with 2 spaces, 4 spaces or tabs, the nesting of objects and arrays shows at a glance.

Shrinking a payload

Minify removes every space and line break outside strings. Use it before you paste JSON into a config value or an environment variable.

How to use the JSON Formatter tool

  1. Paste JSON into the left editor. Upload File loads a .json file, and Load URL fetches from an endpoint that allows cross-origin requests.
  2. Pick the indentation from the dropdown: 2 Spaces, 4 Spaces or Tab.
  3. Click Format. The right panel shows the beautified JSON, each panel header shows the length in characters, and the gutter numbers every line.
  4. Invalid input shows an error such as "Error at line 3:" followed by the browser's own message, for example "Unexpected token } in JSON at position 42". Fix it on the left and click Format again.
  5. Click Minify instead when you need the smallest output, and Copy Result to put the result on the clipboard.

How to format and validate JSON in code

JavaScript / Node.js

const raw = '{"id":1,"name":"John","roles":["admin","user"]}';

// Validate + pretty-print with 2-space indentation
try {
  const data = JSON.parse(raw);
  console.log(JSON.stringify(data, null, 2));
} catch (err) {
  console.error("Invalid JSON:", err.message);
}

// Minify
console.log(JSON.stringify(JSON.parse(raw)));

JSON.stringify's third argument is the indent: a number of spaces or a string such as "\t".

PHP

<?php
$raw = '{"id":1,"name":"John","roles":["admin","user"]}';

$data = json_decode($raw, true);
if (json_last_error() !== JSON_ERROR_NONE) {
    die('Invalid JSON: ' . json_last_error_msg());
}

echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

JSON_PRETTY_PRINT always uses 4 spaces. Add JSON_THROW_ON_ERROR (PHP 7.3+) to get an exception instead of checking json_last_error().

Python

import json

raw = '{"id":1,"name":"John","roles":["admin","user"]}'

try:
    data = json.loads(raw)
except json.JSONDecodeError as e:
    raise SystemExit(f"Invalid JSON at line {e.lineno} col {e.colno}: {e.msg}")

print(json.dumps(data, indent=2, ensure_ascii=False))

# From the shell, without writing a script:
#   python3 -m json.tool input.json

Common errors and how to fix them

Unexpected token } in JSON at position N
A trailing comma before the closing brace or bracket. JSON does not allow trailing commas, so remove the comma after the last property or array item.
Unexpected token ' in JSON
Strings and keys must use double quotes. Single-quoted strings are valid JavaScript but not valid JSON.
Unexpected token / in JSON
JSON has no comments. Remove // and /* */ comments, or use a format that supports them such as JSON5 or YAML.
Unexpected end of JSON input
The document is truncated: a closing brace, bracket or quote never arrived. Check whether a log cut the output short or a response was only partly streamed.
Unexpected token N / undefined is not valid JSON
NaN, Infinity and undefined are not JSON values. Replace them with null or a number before serializing.

Good to know

JSON (RFC 8259) is a strict subset of JavaScript's object syntax. Keys must be double-quoted strings, and values can only be objects, arrays, strings, numbers, true, false and null. There are no comments and no trailing commas. Most parse errors come from pasting a JavaScript object literal and expecting it to be JSON.

Keep the original file when it carries 64-bit IDs as bare numbers. Formatting is a parse-and-reserialize step, so a few surface details change even though the meaning does not. 1.0 comes back as 1, escapes such as \u00e9 are written as the plain character, and integers above 2^53 lose precision. Integer-like keys such as "2" also move to the front of their object.

Specifications and references

This tool follows the specifications below. Read them when you need the exact normative wording, for example to settle an edge case between two implementations.

Guides and tutorials

Frequently Asked Questions

Does the formatter validate my JSON?

Yes. Invalid JSON shows an error with the line where parsing stopped and the browser's description of the problem. The usual causes are a missing comma, a trailing comma, or single quotes where JSON requires double quotes.

Can I minify instead of beautify?

Yes. The Minify button removes every space and line break outside strings, which gives the smallest output for storage or transmission.

Which indentation options are available?

The dropdown offers 2 Spaces, 4 Spaces or Tab. Pick the one your project's style guide uses.

Does it change the order of keys?

Ordinary keys keep their original order, and only whitespace and line breaks change. However, integer-like keys such as "2" and "10" move to the front in ascending order, because that is how JavaScript objects store them.

Can I format a JSON file from a URL?

Yes. Click Load URL and enter a JSON endpoint; the fetch works when the server allows cross-origin requests. Otherwise, save the response and use Upload File.

Next steps

Once the JSON parses, convert it: to CSV, to YAML, to a PHP array, to a JavaScript object. Or read it as a grid in the JSON table viewer.

More Code Formatters & Beautifiers

Browse all code formatters & beautifiers

Copied to clipboard!