Home / Developer tools / JSON formatter

JSON Formatter

Turn JSON into clean, indented text that is easy to read and review. Choose the indent, sort the keys, and get a clear error message if the JSON is broken.

Try:

The work is done in your browser. Your JSON is not sent to any server.

What the formatter does

It parses your JSON and writes it back with one value per line and consistent indentation. The data does not change. This is the layout you want for code review, documentation, a config file in git or a quick look at an API response.

Options

  • Indent: 2 spaces (the common choice for JSON), 4 spaces, 8 spaces or a tab.
  • Sort keys: orders the keys of every object A to Z, which makes two files easy to compare.
  • Short arrays on one line: keeps lists of simple values, such as [1, 2, 3], on a single line instead of one number per line.
  • Accept comments and trailing commas: for config files in JSONC style. They are removed, and the tool tells you.

Formatting errors

If the JSON is not valid, the tool shows the line and column of the first problem, with the line and a marker under the character. The usual causes are a trailing comma, single quotes, a missing quote around a key and a missing comma between items. See the JSON validator for the full list.

Why format JSON

  • Review and version control. One value per line gives clean diffs in git, so a change shows up as a changed line, not a changed file.
  • Consistency. Everyone on a team gets the same layout, whatever editor they use.
  • Debugging. Structure errors are easier to see in indented text.
  • Documentation. API examples are easier to follow when they are indented.

How sorting keys helps comparisons

Two JSON files with the same data can list their keys in different orders, and a text diff then shows many changes that are not real. Sort the keys of both files and the diff shows only what changed. For a comparison that ignores order, use the JSON diff tool.

What the formatter does not change

Values, key order (unless you sort), number text such as 1.50, and the text of every string stay as they are. The one rewrite is that an escape sequence inside a string may be written in its shortest form, for example \u00e9 becomes the letter é.

2 spaces, 4 spaces or a tab?

IndentCommon inGood for
2 spacesJavaScript and web projects, package.jsonDeeply nested data that stays narrow
4 spacesPython json.tool, many Java and PHP teamsEasy scanning of shallow data
TabTeams that let each person set the tab widthPersonal and accessibility preferences
8 spacesRareVery flat data, or teaching

Short arrays on one line

Without the option, every array item gets its own line:

{
  "size": [
    1,
    2,
    3
  ]
}

With it, a list of simple values that fits in 80 characters stays on one line:

{
  "size": [1, 2, 3]
}

Lists that contain objects or arrays are always expanded.

Format JSON in your editor and tools

  • VS Code: open the file and run Format Document (Shift+Alt+F on Windows and Linux, Shift+Option+F on macOS).
  • Prettier: npx prettier --write data.json
  • Browser DevTools: the Network tab shows a formatted preview of JSON responses.
  • PHP: json_encode($data, JSON_PRETTY_PRINT) indents with 4 spaces.

Format, beautify, minify: what is the difference?

  • Format and beautify both add line breaks and indentation. This page adds layout options, and the beautifier adds a colored view with line numbers.
  • Minify does the opposite and removes all optional whitespace to save bytes.

On the command line

jq . data.json
jq -S . data.json          # sort keys
python3 -m json.tool --indent 2 data.json
node -e "console.log(JSON.stringify(JSON.parse(require('fs').readFileSync(0,'utf8')),null,2))" < data.json

Next steps: minify the result for production, or compare two JSON files. The JSON minification guide explains when each layout is right. Your JSON is processed in your browser and is not uploaded.

Frequently asked questions

How do I format JSON?

Paste it into the box and choose an indent. The formatted JSON appears below. Press Copy or Download.

How many spaces should I use to indent JSON?

Two spaces is the most common choice. Four is also popular. Use a tab if your team prefers it. JSON itself does not care.

Why does the formatter show an error?

The JSON is not valid. The error tells you the line and column. Common causes are a trailing comma, single quotes or a missing comma.

Does sorting keys change the meaning of JSON?

For most programs, no, because objects are unordered. A few programs do depend on key order, so check before you sort a file you will send somewhere.

What is the difference between a JSON formatter and a validator?

A formatter changes the layout of valid JSON. A validator checks that the JSON is valid. This formatter also reports errors, and the validator adds warnings for duplicate keys and big numbers.

Can I format JSON that has comments?

Yes. Leave Accept comments and trailing commas on, and they are removed in the result.