Development 6 min read
If you have ever worked with an API, a settings file or a data export, you have met JSON. It looks simple, yet one misplaced comma is enough to make a whole file invalid and produce a cryptic error message.
In this guide you will learn what JSON is, which value types it supports, the syntax rules to remember, the most common mistakes with before and after examples, and how to validate, format and minify your data in seconds.
Validate and format your JSON now Paste your JSON to find errors, pretty print or minify it. Everything runs in your browser and nothing is uploaded. Open the JSON formatterWhat is JSON?
JSON (JavaScript Object Notation) is a lightweight text format for storing and exchanging data. Data is organized as key and value pairs, for example {"name": "Ana", "age": 30}. It is easy for people to read and easy for programs to parse.
Although it came from JavaScript, JSON is language independent. It is the standard way web APIs send data between servers and apps, and it is also widely used in configuration files, logs and data exports.
The six value types
Every value in JSON is one of these six types:
- String: text between double quotes, such as "hello".
- Number: integers or decimals, such as 42 or 3.14, without quotes.
- Boolean: true or false, in lowercase and without quotes.
- Null: the word null, meaning "no value".
- Array: an ordered list between square brackets, such as [1, 2, 3].
- Object: a set of key and value pairs between curly braces, such as {"city": "Lisbon"}. Keys are always strings.
Arrays and objects can contain other arrays and objects, so you can describe data of any depth.
The syntax rules to remember
JSON is much stricter than JavaScript. Most errors come from breaking one of these rules:
- Strings and keys must use double quotes. Single quotes are not allowed.
- No trailing commas: the last item of an array or object must not be followed by a comma.
- No comments: neither // nor /* */ is valid.
- Only the six value types exist: undefined, NaN, Infinity and functions are not valid.
- Pairs are separated by commas, and a key is separated from its value by a colon.
- Every opening brace or bracket needs a matching closing one.
The most common errors and how to fix them
These five mistakes cause the vast majority of "invalid JSON" messages:
- Trailing comma. Before: {"a": 1, "b": 2,}. After: {"a": 1, "b": 2}. Remove the comma after the last item.
- Single quotes. Before: {'name': 'Ana'}. After: {"name": "Ana"}. Replace every single quote with a double quote.
- Unquoted key. Before: {name: "Ana"}. After: {"name": "Ana"}. Put every key inside double quotes.
- Missing bracket or brace. Before: {"items": [1, 2, 3}. After: {"items": [1, 2, 3]}. Close every array and object you open.
- Wrong escape of quotes. Before: {"text": "He said "hi""}. After: {"text": "He said \"hi\""}. A double quote inside a string must be escaped with a backslash. The same goes for a backslash, which is written \\.
Also watch for pasted "smart quotes" from word processors: they look like quotes but are not valid in JSON.
How to read a JSON error message
Most parsers tell you where they got lost, with a message such as "Unexpected token } in JSON at position 25" or "Expected double-quoted property name at line 3 column 5". The position is a character count from the start, while line and column point to the exact spot.
The real mistake is often just before the reported position. An unexpected closing brace usually means there is a trailing comma right before it, and an unexpected end of data usually means a missing closing bracket. Look at the line reported and the one above it.
How to validate, format and minify JSON
- Open the free JSON formatter. It runs entirely in your browser, so your data never leaves your device.
- Paste your JSON. If it is invalid, the tool shows the error with its line and column so you can fix it.
- Use pretty print to indent the data and make it readable, or minify to remove spaces and reduce the size before sending it.
- Copy the result. If you changed a file and want to see exactly what is different, compare both versions with the text diff tool.
JSON vs XML vs YAML
XML is more verbose, with opening and closing tags, but supports attributes and is still common in older systems. YAML is cleaner for humans and allows comments, which is why it is popular in configuration files, but its indentation rules are easy to get wrong.
JSON sits in the middle: compact, unambiguous and supported by practically every language, which makes it the default choice for APIs. Its main drawback is the lack of comments.
Practical tips
- Always validate JSON before sending it or saving it into a configuration file.
- Use an editor with JSON highlighting, which shows mismatched quotes and brackets right away.
- Do not write JSON by hand when you can generate it from code, as libraries escape characters correctly.
- If a URL parameter carries JSON, encode it first with the URL encoder so special characters do not break the request.
- Never put passwords or tokens in JSON you paste into online tools that upload data. Prefer tools that work locally.
Conclusion
JSON is simple once you know its rules: six value types, double quotes, no trailing commas and no comments. When something breaks, read the line and column of the error, check the spot just before it and let a validator do the heavy lifting. A few seconds with a formatter saves a lot of debugging time.
Frequently asked questions
What is JSON used for?
It is used to exchange data between applications, especially web APIs, and to store settings and structured data in files. Almost every programming language can read and write it.
Can JSON have comments?
No. Standard JSON does not allow comments. If you need notes, add a regular field such as "comment" or use a format like YAML for configuration files.
Why does my JSON say unexpected token?
The parser found a character it did not expect, usually a trailing comma, a single quote, an unquoted key or a missing bracket. Check the position reported and the line just above it.
What is the difference between formatting and minifying JSON?
Formatting (pretty print) adds line breaks and indentation so people can read it. Minifying removes all unnecessary spaces to make the file smaller. The data itself is exactly the same.
Is it safe to use an online JSON formatter?
It is safe when the tool works in your browser and does not send the content to a server. Our JSON formatter processes everything locally on your device.
Free tools mentioned in this article
Keep reading
- How to Create a Strong PasswordLength, randomness and uniqueness: the simple rules that actually protect your accounts, and how to follow them without memorizing a thing.
- The Pomodoro Technique: Complete GuideWork in focused 25-minute blocks with short breaks. Learn how the method works, how to start today and how to adapt it to your routine.
- What Is a Hash? MD5, SHA-256 and What They Are Used ForA hash is a short fingerprint of any data. Learn how it works, which algorithms to trust and how to use hashes to verify files and protect passwords.