How to Format JSON: Validate, Prettify & Fix Broken JSON
JSON (JavaScript Object Notation) is everywhere. APIs return it, configuration files use it, databases store it, and developers pass it between services dozens of times a day. But raw JSON is often unreadable β a single line of deeply nested data with no indentation and no line breaks. Worse, a single misplaced comma or missing quote can break the entire structure.
This guide covers everything you need to know about formatting, validating, and fixing JSON. You will learn the most common syntax errors, how to spot and fix them, and how to use our free tool to clean up messy JSON instantly.
Try the Free JSON Formatter NowWhat Is JSON?
JSON is a lightweight data interchange format that is easy for both humans and machines to work with. It was derived from JavaScript but is language-independent β virtually every programming language has built-in support for parsing and generating JSON.
A JSON document is built from two structures:
- Objects β unordered collections of key-value pairs, wrapped in curly braces
{ }. - Arrays β ordered lists of values, wrapped in square brackets
[ ].
Values can be strings (in double quotes), numbers, booleans (true or false), null, objects, or arrays. These can be nested to any depth, which is what makes JSON flexible enough to represent complex data structures.
Here is a simple example:
{
"name": "Tools Oasis",
"type": "website",
"tools": 30,
"categories": ["image", "pdf", "code", "seo"],
"free": true
}
Common JSON Errors and How to Fix Them
JSON has a strict syntax. Even small deviations will cause parsers to reject the entire document. Here are the most frequent errors developers encounter:
Trailing Commas
This is the number one JSON error. JavaScript allows trailing commas in arrays and objects, but JSON does not.
// INVALID - trailing comma after last item
{
"name": "test",
"value": 42,
}
// VALID
{
"name": "test",
"value": 42
}
Fix: Remove the comma after the last property or array element.
Single Quotes Instead of Double Quotes
JSON requires double quotes for all strings and keys. Single quotes are not valid JSON, even though they work in JavaScript and Python.
// INVALID
{'name': 'test'}
// VALID
{"name": "test"}
Fix: Replace all single quotes with double quotes.
Unquoted Keys
In JavaScript, object keys do not need quotes. In JSON, every key must be a double-quoted string.
// INVALID
{name: "test", value: 42}
// VALID
{"name": "test", "value": 42}
Fix: Wrap all keys in double quotes.
Comments
JSON does not support comments of any kind β no // single-line comments and no /* */ block comments. This is one of the most common complaints about the format.
// INVALID
{
// user settings
"theme": "dark"
}
Fix: Remove all comments. If you need comments in configuration files, consider using JSONC (JSON with Comments) or YAML instead, but be aware that standard JSON parsers will reject them.
Missing or Extra Brackets
Unbalanced braces { } or brackets [ ] will break parsing. This is common in deeply nested structures where it is easy to lose track of nesting levels.
Fix: Use a JSON formatter to identify where the mismatch occurs. The error message usually points to the approximate location.
Incorrect Data Types
Numbers should not be quoted (unless you want them as strings), booleans must be lowercase (true, not True or TRUE), and null must be lowercase.
// INVALID
{"active": True, "count": "42", "data": Null}
// VALID
{"active": true, "count": 42, "data": null}
How to Validate JSON
Validation checks whether your JSON is syntactically correct β it answers the question "is this valid JSON?" before you try to use it in your application.
There are several ways to validate JSON:
- Online validators β paste your JSON into a tool that checks syntax and reports errors with line numbers. Our JSON Formatter validates automatically as you paste.
- Browser console β run
JSON.parse(yourString)in the developer console. If it throws a SyntaxError, the JSON is invalid. - Code editors β VS Code, Sublime Text, and other editors highlight JSON syntax errors in real time when you open a
.jsonfile. - Command line β pipe JSON through
jq .orpython -m json.tool. If the command fails, the JSON is malformed.
Always validate JSON before sending it to an API, storing it in a database, or committing it to version control. Catching errors early saves debugging time later.
How to Use Our JSON Formatter
Our JSON Formatter is a free, browser-based tool that formats, validates, and helps you fix JSON in seconds. Here is how to use it:
- Paste your JSON β copy the raw or minified JSON into the input area. You can paste anything from a single-line API response to a large configuration file.
- Format it β click the Format button. The tool parses your JSON, validates the syntax, and outputs a properly indented, human-readable version.
- Check for errors β if the JSON is invalid, the tool shows an error message indicating what went wrong and approximately where the problem is.
- Copy the result β use the copy button to grab the formatted JSON and paste it wherever you need it.
All processing happens entirely in your browser. Your data is never sent to a server, making it safe to format JSON containing API keys, tokens, or other sensitive information.
API Debugging Workflow with JSON Formatting
One of the most common uses for a JSON formatter is debugging API responses. Here is a practical workflow that developers use daily:
- Make your API request β use your HTTP client (Postman, curl, fetch, or your app's code) to call the endpoint.
- Copy the response body β API responses are typically minified, making them hard to read.
- Paste into the formatter β our JSON Formatter instantly prettifies the response so you can see the structure.
- Inspect the data β with proper indentation, you can quickly find the nested property you are looking for, verify data types, and check for unexpected null values.
- Compare with expected output β if the response does not match expectations, use our Text Diff Tool to compare the actual response with the expected one and identify exactly what differs.
This workflow is much faster than scrolling through a wall of unformatted text or adding console.log statements to your code.
JSON Formatting vs. jq on the Command Line
Developers who work heavily in the terminal often use jq, a powerful command-line JSON processor. How does our online formatter compare?
- jq is excellent for scripting, piping API responses, filtering and transforming JSON data, and integrating into shell workflows. If you need to extract a specific field from a JSON array or transform data structures, jq is the right tool.
- Our online formatter is ideal for quick visual inspection, sharing formatted JSON with colleagues who do not use the command line, validating JSON copied from documentation or emails, and working on machines where jq is not installed.
Both tools serve different workflows and complement each other. The online formatter excels at visual validation β quickly seeing the structure of your data and spotting errors β while jq excels at programmatic manipulation.
Working with Related Formats
JSON is not the only structured data format you will encounter. Here are related tools that complement the JSON Formatter:
- XML β many legacy APIs and SOAP services return XML. Use our XML Formatter to prettify XML responses.
- HTML, CSS, and JavaScript β our HTML/CSS/JS Formatter handles formatting for front-end code.
- Base64-encoded JSON β some APIs return JSON payloads encoded in Base64 (common in JWTs and webhooks). Use our Base64 Encoder/Decoder to decode the payload first, then format the resulting JSON.
Wrapping Up
JSON is deceptively simple β its syntax has very few rules, but breaking any of them causes the entire document to fail. The most common culprits are trailing commas, single quotes, unquoted keys, and comments. A good JSON formatter catches these errors instantly, saves you from frustrated debugging, and makes complex data structures easy to read and navigate.
Whether you are debugging an API, reviewing a configuration file, or cleaning up data for a colleague, formatting your JSON should be your first step.
Got messy JSON?
Try the Free JSON Formatter Now