Skip to content
UtiliaTools

How to Format JSON: The Complete Guide for Developers

Master JSON formatting with practical tips, common error fixes, and a free online JSON formatter to beautify your data in seconds.

How to Format JSON: The Complete Guide for Developers

JSON (JavaScript Object Notation) is the universal language of data exchange on the web. Whether you're building a REST API, configuring a application, or transferring data between services, you'll work with JSON constantly. But raw JSON can be hard to read, and even small syntax errors can break your entire application.

This guide covers everything you need to know about formatting JSON — from basic structure to advanced tips and common pitfalls.

What Is JSON?

JSON is a lightweight, text-based data format that's easy for both humans to read and machines to parse. It's built on two structures:

  • Key-value pairs (objects): { "name": "Alice", "age": 30 }
  • Ordered lists (arrays): [1, 2, 3, "four"]

These can be nested to create complex data structures:

{
  "user": {
    "name": "Alice",
    "roles": ["admin", "editor"],
    "settings": {
      "theme": "dark",
      "notifications": true
    }
  }
}

Why Format JSON?

When JSON is minified (all whitespace removed), it becomes nearly impossible to read:

{"user":{"name":"Alice","roles":["admin","editor"],"settings":{"theme":"dark","notifications":true}}}

Formatting (also called "beautifying" or "pretty-printing") adds indentation and line breaks to make the structure visible:

{
  "user": {
    "name": "Alice",
    "roles": [
      "admin",
      "editor"
    ],
    "settings": {
      "theme": "dark",
      "notifications": true
    }
  }
}

Formatted JSON is essential for:

  • Debugging API responses: Spot missing fields or unexpected values quickly.
  • Code reviews: Make configuration files readable for your team.
  • Documentation: Include clear JSON examples in your docs.
  • Manual editing: Find and fix errors without losing your mind.

JSON Formatting Best Practices

1. Use Consistent Indentation

The standard is either 2 spaces or 4 spaces per level. Pick one and stick with it across your project. Most style guides (including Google's and Airbnb's) recommend 2 spaces.

2. Always Quote Keys

JSON requires double quotes around all object keys. This is a common mistake when transitioning from JavaScript:

// ❌ Invalid JSON
{ name: "Alice" }

// ✅ Valid JSON
{ "name": "Alice" }

3. Use Double Quotes for Strings

JSON strings must use double quotes, not single quotes:

// ❌ Invalid
{ 'name': 'Alice' }

// ✅ Valid
{ "name": "Alice" }

4. No Trailing Commas

Unlike JavaScript, JSON does not allow trailing commas:

// ❌ Invalid
{ "name": "Alice", "age": 30, }

// ✅ Valid
{ "name": "Alice", "age": 30 }

5. Valid Data Types Only

JSON supports exactly six data types:

Type Example
String "hello"
Number 42, 3.14
Boolean true, false
Null null
Object { "key": "value" }
Array [1, 2, 3]

There's no undefined, Date, function, or Symbol in JSON.

How to Format JSON Programmatically

JavaScript

const data = { name: "Alice", age: 30 };
const formatted = JSON.stringify(data, null, 2);
console.log(formatted);

The third parameter (2) sets the indentation to 2 spaces.

Python

import json
data = {"name": "Alice", "age": 30}
formatted = json.dumps(data, indent=2)
print(formatted)

Command Line (jq)

echo '{"name":"Alice"}' | jq .

Common JSON Errors and How to Fix Them

Error Cause Fix
Unexpected token ' Single quotes instead of double Replace ' with "
Unexpected end of JSON input Truncated or empty data Check that the JSON is complete
Unexpected token , Trailing comma Remove the last comma
Unexpected token u undefined in the data Replace with null or remove the key

Validate Before You Format

Formatting won't fix invalid JSON. If your JSON has syntax errors, a formatter may produce unexpected results or fail entirely. Always validate your JSON first, then format it.

A good formatter will both validate and beautify your JSON in one step, highlighting any errors with clear line numbers and descriptions.

Try Our Free JSON Formatter

Our JSON Formatter handles all of this for you. Paste your JSON, and it will:

  • Automatically detect and fix common formatting issues
  • Beautify your JSON with configurable indentation
  • Validate syntax and highlight errors
  • Work entirely in your browser — your data never leaves your device

Whether you're debugging a tricky API response, cleaning up a config file, or preparing JSON for documentation, our formatter makes it instant and error-free.

JSON vs. XML vs. YAML

If you're choosing a data format for your project, here's a quick comparison:

Feature JSON XML YAML
Readability Good Verbose Excellent
File size Small Large Medium
Parsing speed Fast Slow Medium
Data types Limited None (all strings) Rich
Comments Not supported Supported Supported

JSON remains the best choice for APIs and web data exchange due to its balance of simplicity, speed, and universal support.

Conclusion

Properly formatted JSON saves time, reduces bugs, and makes your code more maintainable. Follow the best practices above, validate before formatting, and use the right tools to speed up your workflow.

Try the JSON Formatter now — paste your JSON and see the difference formatting makes in seconds.

Last updated on 2026-09-27