JSON Formatter Guide: How to Validate, Beautify and Fix JSON
Messy or broken JSON can waste hours of debugging. Learn how JSON works, the syntax errors that trip people up most, and how to validate and beautify JSON in seconds.
If you work with websites, apps, payment gateways or almost any modern API, you will meet JSON every day. It is the format servers use to send data back and forth, configuration files use to store settings, and developers use to share sample payloads. When it is neatly formatted it is easy to read. When it arrives as one long line, or has a single missing comma, it can be maddening.
This guide covers what JSON is, the errors that break it most often, and how to use our free JSON Formatter & Validator to beautify and check JSON quickly.
What is JSON?
JSON stands for JavaScript Object Notation. Despite the name, it is language-independent; PHP, Python, Java, Kotlin, Swift and practically every other language can read and write it. A JSON document is built from a few simple pieces:
- Objects: curly braces holding key–value pairs, such as
{"name": "Asha"}. - Arrays: square brackets holding an ordered list, such as
[1, 2, 3]. - Strings: text in double quotes.
- Numbers: integers or decimals, without quotes.
- Booleans and null:
true,falseandnull, in lowercase.
Here is a small, valid example of an order as an API might return it:
{
"order_id": "BB-10234",
"customer": {
"name": "Ravi Kumar",
"city": "Patna"
},
"items": [
{ "sku": "TSHIRT-M", "qty": 2, "price": 499 },
{ "sku": "CAP-01", "qty": 1, "price": 299 }
],
"paid": true,
"coupon": null
}
Common JSON syntax errors and how to fix them
JSON is strict. Things that are acceptable in JavaScript or in a casual note will make a JSON parser fail. These are the mistakes we see most often:
| Error | Invalid example | Fix |
|---|---|---|
| Trailing comma | {"a": 1, "b": 2,} | Remove the comma after the last item |
| Single quotes | {'a': 'yes'} | Use double quotes for keys and strings |
| Unquoted keys | {a: 1} | Wrap keys in double quotes: {"a": 1} |
| Missing comma | {"a": 1 "b": 2} | Add a comma between pairs |
| Comments | {"a": 1 // note} | Remove comments; standard JSON does not allow them |
| Capitalised literals | {"ok": True} | Use lowercase true, false, null |
| Unescaped quotes | {"note": "He said "hi""} | Escape inner quotes: \"hi\" |
| Mismatched brackets | {"items": [1, 2} | Close every [ with ] and every { with } |
Two more subtle problems: copying JSON from a chat app or word processor can replace straight quotes with "smart" curly quotes, which are not valid; and numbers with leading zeros, such as 007, are not allowed. If a value like a PIN code or phone number must keep its leading zero, store it as a string.
How to validate and beautify JSON online
A formatter (or beautifier) adds indentation and line breaks so the structure is easy to scan. A validator checks that the JSON follows the rules and points you towards the problem when it does not. Our free JSON formatter and validator does both:
- Open the JSON Formatter & Validator.
- Paste your JSON, whether it is a minified API response, a config file or a snippet from documentation.
- Run the format or validate action.
- If the JSON is valid, you will get a cleanly indented version you can read or copy.
- If it is invalid, look at the reported error and the area around it, fix it, and validate again.
Reading error messages
Parsers usually report where they noticed the problem, which is not always where the mistake is. A missing closing brace near the top may only be reported at the very end. If the reported line looks fine, check the line just before it for a missing comma, quote or bracket.
Debugging API responses with JSON
When an integration breaks, whether it is a payment gateway, a shipping partner, a WhatsApp API or your own mobile app's backend, the JSON is usually the best clue. Some practical habits:
- Look at the raw response first. Before assuming your code is wrong, beautify the response and confirm the field you expect is actually there, with the name and type you expect.
- Check types, not just values.
"499"(a string) and499(a number) look similar but behave differently in code, especially in comparisons and arithmetic. - Watch for null and missing keys. An optional field may be
nullin one response and absent in another. Handle both. - Confirm it is JSON at all. If parsing fails on the first character, the server may have returned an HTML error page, such as a 500 error or a login page, instead of JSON.
- Remove secrets before sharing. API keys, tokens and customer details should be removed before pasting a payload into a ticket, chat or any third-party tool.
Here is a typical error response worth reading carefully:
{
"status": "error",
"code": 422,
"errors": {
"phone": ["The phone field must be 10 digits."]
}
}
Formatted like this, the problem is obvious. Squashed onto one line among hundreds of other characters, it is easy to miss.
Minified vs formatted JSON
Minified JSON removes all unnecessary whitespace, which keeps payloads small for transfer between servers and apps. Formatted JSON is for humans. Both contain exactly the same data. In practice:
- Use minified JSON in production APIs and stored data where size matters.
- Use formatted JSON in config files, documentation, code reviews and while debugging.
Most languages can do either. In PHP, for example, json_encode($data, JSON_PRETTY_PRINT) produces formatted output, and in JavaScript JSON.stringify(data, null, 2) does the same.
Frequently asked questions
Can JSON contain comments?
Standard JSON does not allow comments, and most parsers will reject them. Some tools accept variants such as JSONC for configuration files, but if data is going to an API, remove all comments.
What is the difference between a JSON formatter and a validator?
A formatter makes valid JSON readable by adding indentation and line breaks. A validator checks whether the JSON is correct according to the syntax rules. Our tool combines both, so you can check and tidy JSON in one place.
Is JSON the same as a JavaScript object?
They look similar, but JSON is stricter. JSON requires double-quoted keys and strings, does not allow trailing commas, functions or comments, and only supports a fixed set of value types.
Need help with APIs and integrations?
Connecting payment gateways, CRMs, shipping partners or a mobile app backend is where JSON problems tend to pile up. Our team handles web development and API integrations as well as Android and iOS app development, and we are happy to help untangle an integration that is not behaving.
BaalBo Infotech is based in Patna and works with clients across India. Contact us to talk through what you are building.
Written by
Sukesh Kumar
Founder, CEO & Director, BaalBo Infotech
Sukesh founded BaalBo Infotech in Patna in 2023 and leads a team building websites, apps and online stores for businesses in India and abroad.
More about Sukesh