Skip to content
Developer GuideJSON & Data

How to Format JSON

Learn how to format JSON, compare pretty-printed and minified output, fix common syntax errors, and format JSON in JavaScript or Python.

What is JSON formatting?

JSON formatting, often called pretty-printing or beautifying JSON, adds indentation and line breaks to valid JSON text. The structure and values stay the same, but nested objects and arrays become much easier to scan.

Compact JSON is useful for transport, while formatted JSON is usually better for reading, debugging, code reviews, configuration work, and API inspection.

Why format JSON?

  • Reveal the hierarchy of nested objects and arrays.
  • Make missing values or unexpected fields easier to notice.
  • Compare API responses and configuration files more efficiently.
  • Reduce mistakes when copying or editing test data.
  • Produce readable examples for documentation and issue reports.

How to format JSON online

  1. Open the JSON Formatter.
  2. Paste compact or inconsistently spaced JSON into the input field.
  3. Select Format JSON.
  4. Review the two-space indented output.
  5. Copy the formatted result when it is ready.

Orbilyra performs formatting locally in the browser. The formatter parses the input before producing output, so invalid syntax is reported instead of being silently changed.

JSON pretty print example

Compact input

{"name":"Orbilyra","tools":["formatter","validator"],"free":true}

Pretty-printed output

{
  "name": "Orbilyra",
  "tools": [
    "formatter",
    "validator"
  ],
  "free": true
}

Both examples represent the same data. The second version makes the array and property boundaries visible at a glance.

Minified vs formatted JSON

Formatted JSON includes optional whitespace for people. Minified JSON removes unnecessary spaces and line breaks to produce a compact representation for transport, fixtures, or storage where readability is not the priority.

Formatting and minification should not alter the parsed values. They only change the text representation around those values.

Common JSON formatting errors

  • Trailing commas: JSON does not allow a comma after the last item.
  • Single quotes: JSON strings and property names require double quotes.
  • Unquoted keys: every object property name must be a JSON string.
  • Missing delimiters: braces and brackets must be correctly paired.
  • Unsupported values: undefined, functions, and comments are not JSON.
  • Copied escape sequences: malformed backslashes can make a string invalid.

JSON formatting and validation

Formatting focuses on readable or compact output. Validation focuses on whether the text follows JSON syntax and where parsing fails. A formatter must parse JSON, but a dedicated validator gives syntax checking a clearer workflow.

If the formatter reports an error, open theJSON Validator to inspect the parser message. Syntax validation is different from JSON Schema validation, which checks whether parsed data follows an expected structure and set of rules.

How to format JSON in JavaScript

JavaScript can pretty-print a parsed value with JSON.stringify. The third argument controls indentation:

const value = JSON.parse(compactJson);
const formatted = JSON.stringify(value, null, 2);

console.log(formatted);

JSON.parse throws when the source is invalid. Handle that exception when the input comes from a user, file, API, or other untrusted source.

How to format JSON in Python

Python's standard json module can parse and format JSON without an additional package:

import json

value = json.loads(compact_json)
formatted = json.dumps(value, indent=2, ensure_ascii=False)

print(formatted)

Setting ensure_ascii=False keeps Unicode characters readable instead of escaping every non-ASCII character.

FAQ

Frequently asked questions

What does it mean to format JSON?

Formatting JSON adds consistent indentation and line breaks without changing the parsed keys, values, arrays, or objects.

Does pretty-printing JSON change its data?

No. Pretty-printing changes whitespace only. A parser reads the same data from equivalent compact and formatted JSON.

How many spaces should JSON indentation use?

Two spaces are common for web development, while four spaces are also used. JSON itself does not require a particular indentation width.

Can invalid JSON be formatted?

A formatter must parse the input first, so syntax errors such as trailing commas, single-quoted strings, or missing braces must be fixed before formatting succeeds.

Is a JavaScript object literal always valid JSON?

No. JavaScript object literals may use features JSON does not allow, including comments, unquoted property names, single-quoted strings, undefined, and trailing commas.

Does Orbilyra upload JSON for formatting?

No. The JSON Formatter and JSON Validator process input locally in your browser and do not send it to a server.

← Back to Developer Guides