๐Ÿ“– Tabutility Guide

How to Format JSON: A Developer's Complete Guide

Published 28 July 2026

What Is JSON?

JSON (JavaScript Object Notation) is a lightweight text format for storing and exchanging data. Introduced by Douglas Crockford in the early 2000s and formalised as RFC 8259, it has become the dominant data interchange format for APIs, configuration files, and web applications worldwide.

JSON is language-independent โ€” it can be parsed in Python, Java, Ruby, Go, PHP, Swift, or any other mainstream language. Its simplicity and human-readability made it replace XML in most web API contexts over the past 15 years.

The 6 JSON Data Types

TypeExampleNotes
String"hello"Must use double quotes โ€” never single quotes
Number42, 3.14, -7No distinction between integers and floats
Booleantrue, falseLowercase only โ€” True is invalid
NullnullLowercase only โ€” NULL is invalid
Array[1, "two", true]Ordered list of any JSON values
Object{"key": "value"}Unordered key-value pairs; keys must be strings

Valid JSON โ€” A Complete Example

{
  "user": {
    "id": 1042,
    "name": "Alice Johnson",
    "email": "alice@example.com",
    "active": true,
    "score": 98.5,
    "nickname": null,
    "tags": ["admin", "verified"]
  }
}

The Most Common JSON Errors

1. Trailing commas

The most frequent JSON error. JSON does not allow a comma after the last item in an object or array:

// INVALID โ€” trailing comma after "blue"
{ "colors": ["red", "green", "blue",] }

// VALID
{ "colors": ["red", "green", "blue"] }

2. Single quotes

JSON requires double quotes for both keys and string values. Single quotes are not valid JSON:

// INVALID
{ 'name': 'Alice' }

// VALID
{ "name": "Alice" }

3. Unquoted keys

// INVALID โ€” keys must be quoted strings
{ name: "Alice" }

// VALID
{ "name": "Alice" }

4. Comments

Standard JSON does not support comments. // comment or /* comment */ will cause a parse error. If you need comments in config files, consider JSONC or JSON5 (which require specific parsers), or move to YAML/TOML for configuration.

5. Undefined and functions

JSON has no concept of undefined, functions, dates (as objects), or NaN/Infinity. These JavaScript-specific values cannot be serialised to JSON.

Pretty-Printing vs Minifying

Pretty-printed JSON uses indentation (typically 2 or 4 spaces) and newlines for human readability โ€” ideal for config files, debugging, and version control.

Minified JSON removes all unnecessary whitespace to reduce file size โ€” ideal for API responses and production builds where bandwidth matters.

In JavaScript: JSON.stringify(data, null, 2) pretty-prints with 2-space indentation. JSON.stringify(data) minifies.

A 10KB pretty-printed JSON file typically minifies to 6โ€“7KB โ€” a 30โ€“40% reduction. For large payloads delivered over mobile connections, this matters.

Working With JSON in Code

JavaScript

// Parse (string โ†’ object)
const obj = JSON.parse('{"name":"Alice"}');

// Stringify (object โ†’ string)
const json = JSON.stringify(obj, null, 2); // pretty, 2 spaces

Python

import json

# Parse
obj = json.loads('{"name": "Alice"}')

# Stringify
json_str = json.dumps(obj, indent=2)

Command line (with jq)

# Pretty-print any JSON file
cat data.json | jq '.'

# Extract a field
cat data.json | jq '.user.name'

Validating JSON

JSON validators check that your text conforms to the JSON specification and report the exact location of any errors. You should validate JSON whenever you:

FAQ

What is the difference between JSON and JavaScript objects?

JSON is a text format โ€” it is always a string. JavaScript objects are in-memory data structures. Key differences: JSON requires double quotes around all keys and string values; JS objects allow single quotes and unquoted keys. JSON does not support undefined, functions, Date objects, or comments. JSON.parse() converts a JSON string to a JS object; JSON.stringify() converts a JS object to a JSON string.

Can JSON have comments?

No โ€” standard JSON (RFC 8259) explicitly does not support comments. This was a deliberate design decision by Douglas Crockford to keep the format simple and interoperable. JSON5 and JSONC are unofficial supersets that add comment support, but they require custom parsers and are not universally supported. For config files requiring comments, consider YAML or TOML instead.

What causes 'Unexpected token' errors in JSON?

The most common causes are: (1) trailing commas after the last element in an array or object, (2) single quotes instead of double quotes around keys or strings, (3) unquoted keys, (4) control characters (tab, newline) embedded literally inside strings instead of escaped as \t or \n, and (5) undefined values (which JSON has no equivalent for). Paste into a JSON formatter to immediately identify the line and character position of the error.

What is the difference between pretty-printed and minified JSON?

Pretty-printed JSON uses indentation and newlines for readability โ€” ideal for config files, logs, and debugging. Minified JSON removes all whitespace to reduce file size โ€” typically 30โ€“40% smaller. For APIs, minified is preferred for performance. Use JSON.stringify(data, null, 2) in JavaScript or json.dumps(data, indent=2) in Python for pretty output; omit the indent argument for minified output.

Try the JSON Formatter & Validator Paste, format, validate, and minify JSON instantly in your browser
Open Tool โ†’