Complete Guide to JSON Formatting
If you have spent any time building software that talks to the internet, you have run into JSON. It is everywhere — APIs, config files, databases, even clipboard exports from half the tools you use. This guide covers what JSON actually is, why formatting it matters, and how to do it properly.
What is JSON?
JSON stands for JavaScript Object Notation. Douglas Crockford popularized it in the early 2000s as a lightweight alternative to XML for transmitting structured data between a server and a web application. The spec itself is surprisingly short — you can read the whole thing at json.org in about five minutes.
At its core, JSON gives you six data types: strings, numbers, booleans, null, objects (key-value pairs), and arrays (ordered lists). That is it. No dates, no functions, no comments. The simplicity is the whole point — any programming language can parse it without much effort.
Why Formatting Matters
When a server sends JSON to your browser, it usually strips out all the whitespace. A response that represents a user profile with nested addresses and order history ends up as a single, unbroken line of text. Good luck finding a typo in that.
Formatting (or "pretty printing") adds indentation and line breaks so you can see the structure at a glance. It turns debugging from guesswork into something manageable. You can immediately tell where an object starts and ends, which keys belong to which level, and whether a value is a string or a number.
This matters especially when you are comparing two JSON payloads, writing tests, or trying to figure out why an API response does not match the documentation.
Common Use Cases
- API debugging — Copy a response from DevTools or Postman, paste it into a formatter, and read it properly.
- Configuration files — Package.json, tsconfig.json, and hundreds of other config files use JSON. Keeping them formatted makes them easier to maintain.
- Database inspection — MongoDB, CouchDB, and many other databases store documents as JSON. Formatting helps when you are querying and reviewing records.
- Data pipelines — When you are passing JSON between microservices, being able to quickly validate and format the data at each step prevents hours of head-scratching.
- Documentation — Blog posts, READMEs, and API docs all benefit from well-formatted JSON examples.
JSON Formatting Rules
JSON is strict. If you are used to writing JavaScript objects, some of these rules will trip you up:
- Keys must be double-quoted. Writing
name: "Alice"is valid JavaScript but invalid JSON. It needs to be"name": "Alice". - Strings must use double quotes. Single quotes are not allowed. Neither are template literals.
- No trailing commas. The last item in an array or object must not have a comma after it. This is probably the single most common JSON error.
- No comments. JSON has no syntax for comments. If you need annotated config, look at JSONC or JSON5.
- Numbers cannot have leading zeros.
007is not valid. It must be7. - No undefined or NaN. These JavaScript values do not exist in JSON. Use
nullinstead of undefined.
Formatting Tools
You have plenty of options depending on your workflow:
- Online formatters — Tools like JSONPrettify let you paste JSON and get formatted output instantly, no installation needed.
- CLI tools —
python -m json.toolandjq .are quick ways to format JSON right in the terminal. - Editor extensions — VS Code, Sublime Text, and most editors have built-in JSON formatting or plugins for it.
- Programmatic formatting — In JavaScript,
JSON.stringify(data, null, 2)produces nicely indented output with two-space indentation.
Wrapping Up
JSON formatting is one of those small things that makes a surprisingly big difference in your day-to-day work. Whether you are debugging an API response at 2 AM or reviewing a pull request that touches a config file, having clean and readable JSON saves time and prevents mistakes. Bookmark a good formatter, learn the syntax rules, and you will avoid most of the headaches that come with this format.