JSON prettify and format

Format, validate and fix JSON, with errors by line and column.

loading JSON prettify and format
About this tool

What is a JSON formatter?

A JSON formatter takes JSON that is hard to read, such as a minified API response squeezed into a single line, and prints it again with line breaks and indentation, one key per line, so you can see the structure at a glance. Because it has to read the text before printing it, a formatter is also a validator: if the JSON is broken, you get an error with the line and column where reading stopped instead of a pretty result.

You need one when you debug an API, read a log line, compare two configuration files or paste a payload into a bug report. Pretty JSON makes nesting visible: which array a value belongs to, where an object ends, whether a field is a string "42" or a number 42.

This formatter runs entirely in your browser. The JSON you paste is never uploaded or saved; only your options (indent size, sorting, repair) are remembered on your device. It also does a few things plain JSON.parse cannot: it reads relaxed JSON (comments, single quotes, trailing commas), it can repair broken JSON, it unwraps JSON that arrived escaped inside a string, and it keeps very large numbers exactly as written instead of rounding them.

How to format JSON online

  1. Paste your JSON in the Your raw JSON box on the left. The page opens with a small example; use Clear to empty the box or Load example to bring it back.
  2. The formatted result appears on the right as you type. On a phone, the result is below the input.
  3. Set Indent size (0 to 10 spaces; the default is 3, and 2 is the most common choice in code). An indent of 0 prints the whole document on one line, which works as a quick minifier.
  4. Leave Sort keys on to list the keys of every object in alphabetical order (useful to compare two documents), or turn it off to keep the original order.
  5. If the JSON does not parse, read the red message: it says where the problem is, such as "line 1, column 16". Turn on Repair JSON to let the tool fix missing quotes, commas and brackets; a yellow notice tells you the input was repaired so you check the result before using it.
  6. If you pasted JSON that is wrapped in quotes and full of \", turn on Auto-unescape.
  7. Switch the result between Text and Fold / unfold (a collapsible tree, handy for large documents), then press the copy button.

Examples

Minified API response (default options: indent 3, keys sorted)

Input:

{"user":{"name":"Ana","id":42,"roles":["admin","dev"]},"active":true}

Output:

{
   "active": true,
   "user": {
      "id": 42,
      "name": "Ana",
      "roles": [
         "admin",
         "dev"
      ]
   }
}

Same input, indent 2 and Sort keys off

The key order of the original is kept:

{
  "user": {
    "name": "Ana",
    "id": 42,
    "roles": [
      "admin",
      "dev"
    ]
  },
  "active": true
}

Relaxed JSON with a comment and trailing commas

Strict JSON would reject this config file. The formatter reads it, drops the comment and prints standard JSON (indent 2):

{
  // retry policy
  retries: 3,
  'timeout': 1.5e3,
  hosts: ['a.local', 'b.local',],
}
{
  "hosts": [
    "a.local",
    "b.local"
  ],
  "retries": 3,
  "timeout": 1500
}

Broken JSON fixed with Repair JSON

Input with unquoted keys, single quotes, a missing comma, Python-style True and no closing brace:

{name: 'Ana', tags: ['x' 'y'], ok: True

Output (indent 2), with the "was repaired" notice:

{
  "name": "Ana",
  "ok": true,
  "tags": [
    "x",
    "y"
  ]
}

Big numbers kept exact

JavaScript rounds 12345678901234567890 to 12345678901234567000 when it parses JSON. Here the digits survive: {"orderId": 12345678901234567890, "price": 19.90} becomes "orderId": 12345678901234567890 and "price": 19.9 (a trailing zero after the decimal point is not part of the number's value).

Common errors and how to fix them

  • Invalid JSON at line 1, column 16: a comma is missing between two members, as in {"name": "Ana" "age": 30}. Add the comma at the position shown, or turn on Repair JSON.
  • Invalid JSON at line 1, column 9: two commas in a row, or a comma where a value should be ({"a": 1,, "b": 2}). Remove the extra comma.
  • Invalid JSON at line 1, column 8: the document stops early, usually a missing } or ] at the end of a copied fragment. Copy the whole response, or let Repair JSON close the brackets.
  • Invalid JSON, pointing at the letter u: undefined, NaN or a function are JavaScript, not JSON. Replace them with null or a real value.
  • Invalid JSON at line 1, column 13 in {"price": 1,5}: JSON numbers use a dot as the decimal separator. Write 1.5.
  • The output has "\n" and backslashes everywhere: the JSON was escaped (stored inside a string). Turn on Auto-unescape, which removes up to three layers of escaping.

Frequently asked questions

How do I check if my JSON is valid?

Paste it here. If the formatted result appears, the JSON can be read; if not, the error gives the line and column. For a page focused on validation only, use the JSON validator. Note that this tool also accepts relaxed JSON (comments, single quotes); a strict parser such as JSON.parse or a server may still reject those, so remove them before sending the JSON to an API.

Is it safe to paste private JSON here?

Yes. Formatting happens in your browser and nothing is sent to a server. The text you paste is not stored either: only the options are remembered.

What is the difference between pretty print and minify?

Pretty print adds line breaks and indentation for people; minify removes all optional white space so the JSON is as small as possible for machines. Set the indent to 0 here, or use JSON minify.

Why are my keys in a different order?

Sort keys is on by default and sorts the keys of every object alphabetically. Turn it off to keep the original order. The order of items inside arrays never changes.

Can I see the JSON as a tree?

Yes: choose Fold / unfold above the result to collapse and expand objects and arrays. There is also a dedicated JSON viewer page.

How do I convert JSON to YAML, CSV or XML?

Once your JSON is valid, open JSON to YAML, JSON to CSV or JSON to XML. To find what changed between two versions, use JSON diff.

How to format JSON in code

JavaScript

JSON.stringify takes the number of spaces as its third argument. JSON.parse throws a SyntaxError on invalid JSON (trailing commas and comments included).

const text = '{"user":{"name":"Ana","id":42},"active":true}';
const pretty = JSON.stringify(JSON.parse(text), null, 2);
const minified = JSON.stringify(JSON.parse(pretty));
try {
  JSON.parse('{"a": 1,}');
} catch (err) {
  console.error(err.message); // Expected double-quoted property name in JSON at position 8 ...
}

Python

The standard json module formats, sorts and minifies. ensure_ascii=False keeps accented letters readable.

import json

data = json.loads('{"user":{"name":"Ana","id":42},"active":true}')
print(json.dumps(data, indent=2, ensure_ascii=False, sort_keys=True))
print(json.dumps(data, separators=(",", ":")))  # minified

Go

json.Indent pretty-prints without decoding into a struct, and json.Valid only checks.

text := `{"user":{"name":"Ana","id":42},"active":true}`
var out bytes.Buffer
if err := json.Indent(&out, []byte(text), "", "  "); err != nil {
	fmt.Println("invalid JSON:", err)
}
fmt.Println(out.String())
fmt.Println(json.Valid([]byte(`{"a": 1,}`))) // false

Bash

jq is the usual command-line tool; Python's json.tool works where jq is not installed.

jq . data.json          # pretty print (2 spaces)
jq -S . data.json       # sort keys
jq -c . data.json       # minify
python3 -m json.tool data.json
Report a problem