JSON from an API or a log file often comes as one long line. That is fine for computers. But it is very hard for people to read. Formatting fixes it.
This guide shows you how to format JSON in a few seconds. You can do it online, in VS Code, in JavaScript, in Python or on the command line.
Quick answer: To format JSON, add line breaks and indentation so each value is on its own line. Paste it into an online JSON formatter. Or, in code, use JSON.stringify(data, null, 2) in JavaScript or json.dumps(data, indent=2) in Python.
What does it mean to format JSON?
Formatting is also called pretty printing or beautifying. It changes only the layout of the JSON. It adds:
- Line breaks, so each key and value starts on a new line.
- Indentation, which means spaces at the start of each line. Deeper levels get more spaces, so you can see what sits inside what.
The data itself does not change. Here is the same JSON before and after:
{"id":7,"name":"Desk lamp","tags":["home","light"],"stock":{"shop":3,"online":12}}{
"id": 7,
"name": "Desk lamp",
"tags": [
"home",
"light"
],
"stock": {
"shop": 3,
"online": 12
}
}The opposite of formatting is minifying. That means you remove all the extra spaces and line breaks. This makes the file smaller.
How to format JSON online
The fastest way is an online formatter. There is nothing to install.
- Open the JSON Formatter.
- Paste your JSON, or upload a
.jsonfile. - Pick 2 spaces, 4 spaces or tabs. Then click Format, or press Ctrl + Shift + F.
- Copy the result or download it. Ctrl + Z undoes the formatting.
You can also turn on Sort keys to put every object’s keys in A–Z order. This helps when you compare two versions of a file.
Do you just want to make a one-line API response easy to read? The JSON Pretty Print page opens the same editor. It keeps long numbers exactly as written.
Formatting only works on valid JSON. If your JSON has a mistake, such as a missing comma, fix that first. Our guide on how to validate JSON shows how to find the problem.
How to format JSON in VS Code
- Open the JSON file in VS Code.
- Right-click in the editor and choose Format Document.
- Or use the shortcut: Shift + Alt + F on Windows, Shift + Option + F on Mac, or Ctrl + Shift + I on Linux.
Did you paste JSON into a new, unsaved tab? Then first tell VS Code it is JSON. Click the language name in the bottom-right corner. It may say “Plain Text”. Then choose JSON.
How to pretty print JSON in JavaScript
JSON.stringify() turns a value into JSON text. Its third argument sets the indentation:
const data = { id: 7, name: "Desk lamp", tags: ["home", "light"] };
// 2 spaces
console.log(JSON.stringify(data, null, 2));
// tabs
console.log(JSON.stringify(data, null, "\t"));The second argument, null, means “keep every property”. Do you start with JSON text, not an object? Then read it with JSON.parse() first:
const pretty = JSON.stringify(JSON.parse(text), null, 2);One warning: JSON.parse() stores numbers as JavaScript numbers. So very long whole numbers, such as some IDs, can get rounded. Read why big numbers break in JSON to learn more.
How to pretty print JSON in Python
Python comes with a json module that can do this. Use json.dumps() with the indent option:
import json
data = {"id": 7, "name": "Desk lamp", "tags": ["home", "light"]}
print(json.dumps(data, indent=2))Two extra options are useful:
sort_keys=Truesorts the keys A–Z.ensure_ascii=Falsekeeps letters like é and emoji as they are. Without it, they turn into codes like\u00e9.
To format a JSON file and save the result to a new file:
import json
with open("data.json", encoding="utf-8") as f:
data = json.load(f)
with open("data-pretty.json", "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)How to format JSON on the command line
If Python is installed, you already have a formatter. Run this in a terminal:
python -m json.tool data.jsonIt prints the formatted JSON with 4 spaces of indentation. On some systems, the command is python3 instead.
In Python 3.9 or newer, add --indent 2 for 2 spaces. Add --sort-keys to sort the keys. To save the output to a file, give a second file name:
python -m json.tool data.json pretty.jsonIf the JSON is not valid, the command prints an error with the line and column instead.
Many developers also use jq, a separate command-line tool for JSON. Run jq . data.json to print the file formatted.
How to minify JSON
Sometimes you want the opposite: the smallest file possible. For example, to send it over a network. Use the JSON Minifier, or do it in code:
// JavaScript
JSON.stringify(data);
# Python
json.dumps(data, separators=(",", ":"))Minifying only removes spaces and line breaks outside of strings. The data stays exactly the same. Keep the formatted version for anything people need to read.
Common questions
Is pretty print the same as format?
Yes. “Format”, “pretty print” and “beautify” all mean the same thing. They all add line breaks and indentation so JSON is easy to read.
Does formatting change my JSON data?
No. It only changes the spaces and line breaks between values. The keys, values and their order stay the same. The only exception is if you choose to sort the keys.
Should I use 2 or 4 spaces?
Either is fine. Two spaces is common in JavaScript projects. Python’s json.tool uses 4 by default. Pick one and use it everywhere.
Why won’t my JSON format?
Usually, it is because the JSON is not valid. An extra comma at the end, single quotes or a comment will stop the formatter. Check it with a validator, or see the most common JSON errors.