How to Pretty Print JSON: Make JSON Easy to Read

Pretty printing JSON means displaying it with indentation, spaces, and line breaks so people can read its structure more easily. The data stays the same; only its presentation changes.

By Archana Bhandari ยท

Compact vs pretty printed JSON

Have compact JSON you want to read? Paste it into JSONPlease JSON Formatter to pretty print it directly in your browser. Processing happens locally.

Compact valid JSON:
{"name":"Rahul","city":"Dehradun","skills":["PHP","JavaScript"],"active":true}

Pretty printed valid JSON:
{
  "name": "Rahul",
  "city": "Dehradun",
  "skills": [
    "PHP",
    "JavaScript"
  ],
  "active": true
}

What does pretty print JSON mean?

Pretty printing normally adds new lines, indentation, and consistent spacing. It makes nested objects and arrays easier to see. It should not change the underlying keys, values, or structure.

  • Use it to inspect nested data and API responses.
  • Use it to share readable JSON with another developer.
  • Use it while debugging or reviewing a configuration file.

Pretty printed nested JSON example

Indentation reveals the user object, then its address object and roles array.

{
  "user": {
    "id": 101,
    "name": "Rahul",
    "address": {
      "city": "Dehradun",
      "country": "India"
    },
    "roles": ["editor", "author"]
  }
}

How to pretty print JSON online

Open JSONPlease JSON Formatter, paste compact JSON or use Open JSON for a .json file, choose Format, then review, copy, or download the result. This is useful when you need a readable document quickly.

Pretty print a JSON API response

API responses often arrive on one line. The pretty version below makes the nested data object and orders array much easier to inspect during development.

Compact valid JSON:
{"status":"success","data":{"id":101,"name":"Rahul","orders":[{"id":501,"total":799},{"id":502,"total":1299}]}}

Pretty printed valid JSON:
{
  "status": "success",
  "data": {
    "id": 101,
    "name": "Rahul",
    "orders": [
      { "id": 501, "total": 799 },
      { "id": 502, "total": 1299 }
    ]
  }
}

How to pretty print JSON in JavaScript

JAVASCRIPT, not JSON: JSON.stringify() converts a JavaScript value into JSON text. The first argument is the value to convert, null means no replacer/filter, and 2 asks for two spaces of indentation. The output below is JSON.

const user = {
  name: "Rahul",
  city: "Dehradun"
};

const prettyJSON = JSON.stringify(user, null, 2);
console.log(prettyJSON);

OUTPUT / JSON:
{
  "name": "Rahul",
  "city": "Dehradun"
}

Pretty print JSON with 4 spaces

JAVASCRIPT, not JSON: JSON.stringify(user, null, 4) uses four spaces instead. JSON does not require two or four spaces; teams may choose their own convention.

const prettyJSON = JSON.stringify(user, null, 4);

Pretty print a JSON string in JavaScript

JAVASCRIPT, not JSON: parse JSON text into a JavaScript value first, then stringify it with indentation. JSON.parse() reads JSON text; JSON.stringify() turns a JavaScript value back into JSON text.

const json = '{"name":"Rahul","city":"Dehradun"}';
const data = JSON.parse(json);
const prettyJSON = JSON.stringify(data, null, 2);

What if JSON.stringify() or JSON.parse() fails?

Malformed JSON text can make JSON.parse() fail before you can pretty print it. JSON.stringify() can also fail for some JavaScript values, such as circular references. Validate text first and repair common malformed input if appropriate.

Pretty print JSON in the browser console

JAVASCRIPT, not JSON: console.log(JSON.stringify(data, null, 2)) is a simple way to inspect a value while debugging. Avoid logging sensitive production data.

console.log(JSON.stringify(data, null, 2));

Pretty print JSON on the command line

COMMAND LINE: Python can read and pretty print a valid JSON file with the command below. jq is another popular command-line JSON processor, but it is a separate tool and may not be installed.

python -m json.tool data.json

Pretty print JSON vs format JSON

In normal usage, pretty print JSON and format JSON often mean almost the same thing: both produce a human-readable representation with indentation and line breaks. Format JSON is broader wording; pretty print often describes the readable output or debugging use case.

Pretty print, beautify, and format

These labels are commonly used interchangeably for JSON readability. Different tools use different names, but there is no useful technical distinction to invent between them.

Pretty print JSON vs minify JSON

Pretty print is optimised for human readability. Minify removes unnecessary whitespace for a compact representation. Both can be equally valid JSON.

Pretty printed JSON:
{
  "name": "Rahul",
  "city": "Dehradun"
}

Minified JSON:
{"name":"Rahul","city":"Dehradun"}

Does pretty printing change JSON data?

It should not. Proper pretty printing changes whitespace outside string values, not the keys, values, or structure. Whitespace inside a JSON string is part of the value and must not be changed casually.

{
  "message": "Hello Rahul"
}

Can you pretty print invalid JSON?

Usually a normal JSON pretty printer expects valid JSON. This INVALID JSON has a trailing comma, so a strict formatter can reject it. Check it with a validator, use Repair for common malformed input where appropriate, then format the valid result.

{
  "name": "Rahul",
}

Pretty printing large JSON

Pretty printing makes nested documents easier to inspect, but very large JSON can still become many lines long. A tree viewer can be better for exploring deeply nested data, and search can help find a key or value.

When should you not pretty print JSON?

It is not always necessary. Keep JSON compact when human readability is irrelevant, when another tool already displays it clearly, or when you need a compact representation rather than a debugging view.

  • Do not manually change values while formatting.
  • Do not delete commas or add trailing commas.
  • Do not confuse JavaScript object syntax with JSON.
  • Do not assume indentation is required for valid JSON.

Quick pretty print checklist

Use this before sharing a formatted document.

  • Is the input valid JSON?
  • Do you need it for reading or debugging?
  • Are nested objects and arrays clearly indented?
  • Have keys and values remained unchanged?
  • Has whitespace inside strings remained unchanged?
  • Do you need pretty JSON or minified JSON?

Frequently asked questions

What does pretty print JSON mean? It means showing JSON with readable indentation and line breaks.

How do I pretty print JSON? Use a JSON formatter or JSON.stringify(value, null, 2) in JavaScript.

Is pretty print the same as format JSON? In normal use, they usually mean the same thing.

Is JSON beautify the same as pretty print? Usually, yes.

What does JSON.stringify(obj, null, 2) do? It creates JSON text with two-space indentation.

Can I pretty print invalid JSON? A strict formatter usually cannot; validate or repair it first.

Does pretty printing change JSON data? Proper pretty printing should not.

How many spaces should I use? Two and four are common; JSON requires neither.

How do I pretty print an API response? Copy valid response JSON into a formatter or stringify the parsed value for debugging.

How do I pretty print a JSON file? Open it in a formatter or run python -m json.tool data.json.

Related JSON tools