JSON explained: how to validate, format and fix errors
Learn the rules of valid JSON, how to format it for readability, identify common syntax errors and validate its structure safely.
JSON appears in APIs, configuration files, server responses and data storage. Its syntax is small but strict: one incorrect quote, trailing comma or unsupported value is enough to make a document invalid.
1. What is JSON?
JSON, short for JavaScript Object Notation, is a text format for exchanging structured data. Although its syntax was derived from JavaScript, it is language-independent and can be used from C#, Java, Python, PHP, Go and almost any modern platform.
It can represent objects, arrays, strings, numbers, booleans and null. An object groups name/value pairs, while an array preserves an ordered sequence of values.
{
"name": "Tervix",
"active": true,
"tools": ["JSON formatter", "JSON validator"],
"settings": {
"language": "es",
"indentation": 2
},
"lastError": null
}2. Rules for valid JSON
- Object property names must use double quotes:
"name". - Strings also use double quotes, not single quotes.
- Properties are separated by commas, but a comma is not allowed after the final item.
- A colon must separate a property name from its value:
"active": true. - Supported values are objects, arrays, strings, numbers,
true,falseandnull. - JSON does not support comments,
undefined,NaNorInfinity. - Line breaks inside a string must be escaped as
\\n.
Spaces, tabs and line breaks outside strings do not change the meaning. The same document can therefore be indented for reading or written on one line to reduce its size.
3. How to validate JSON syntax
In JavaScript, JSON.parse() attempts to convert the text into a value. It throws a SyntaxErrorwhen the syntax is invalid, so use it insidetry...catch when the text comes from a user, file or external service.
function validateJson(text) {
try {
const value = JSON.parse(text);
return { valid: true, value, error: null };
} catch (error) {
return {
valid: false,
value: null,
error: error instanceof Error ? error.message : "Invalid JSON",
};
}
}A successful parse confirms that the text follows JSON grammar. It does not confirm required fields, specific types or allowed values; those belong to structural validation.
Error messages may include a position, line or column, but their wording varies between browsers and runtimes. Use the location as a starting point and also inspect the characters immediately before it.
4. How to format and minify JSON
Formatting adds indentation and line breaks so the hierarchy is visible. Minifying removes unnecessary whitespace to create compact output. In both cases, parse the text first: a formatter cannot safely reorganize an invalid document.
const data = JSON.parse(input);
// Formatted with two spaces
const formatted = JSON.stringify(data, null, 2);
// Minified
const minified = JSON.stringify(data);The third argument to JSON.stringify() controls indentation. Two spaces usually balance readability and width; four spaces are more spacious. Without that argument, the output is minified.
undefined, functions or symbols can be omitted, and a circular reference throws an error. Formatting existing valid JSON does not have that issue because it already contains supported types only.5. Common errors and how to fix them
Single-quoted keys or strings
Incorrect: {'name': 'Ana'}. Replace both with double quotes: {"name": "Ana"}.
Unquoted property names
A JavaScript object may be written as{ name: 'Ana' }, but JSON requires{ "name": "Ana" }.
Trailing comma
Remove the comma immediately before } or]. JSON does not allow trailing commas in objects or arrays.
Comments
// comment and /* comment */ are not part of JSON. Store the explanation in a regular property or use a configuration format that explicitly supports comments.
Unsupported values
Replace undefined with null when it represents a missing value. Choose an explicit representation for NaN and Infinity, because JSON does not include them.
Invalid numbers
Avoid leading zeros such as 01, a decimal point without following digits such as 1., and language-specific numeric formats.
Unescaped characters
Inside a string, escape quotes as \\", backslashes as \\\\ and line breaks as\\n.
Duplicate property names
Some parsers keep the last value while others may behave differently. Even when a parser accepts the text, use unique names to avoid data loss and improve interoperability.
6. Validate structure with JSON Schema
Syntax validation answers, “Is this JSON?” JSON Schema answers additional questions: “Is it an object?”, “Does it include these properties?”, “Is this field boolean?” and “Are extra properties allowed?”
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"required": ["name", "active"],
"properties": {
"name": { "type": "string", "minLength": 1 },
"active": { "type": "boolean" }
},
"additionalProperties": false
}This schema requires an object with name andactive, checks their types and rejects undefined fields. Applying it requires a validator compatible with the declared JSON Schema version.
7. A practical method for fixing errors
- Keep a copy of the original text before editing it.
- Run a validator and locate the first reported position.
- Inspect that line and the previous one; a missing quote or comma can shift the reported location.
- Fix one problem at a time and validate again.
- Once syntax is valid, apply readable indentation.
- Visually inspect objects, arrays and value types.
- If a data contract exists, validate against its JSON Schema.
- Run application tests before replacing the original file or response.
For large documents, reduce the case: temporarily remove complete blocks until you isolate the failing section. Avoid global quote or comma replacements without reviewing the result, because they can damage valid strings.
8. Quick validation checklist
- Every property name and string uses double quotes.
- There are no commas after the final item.
- Every property name is followed by a colon.
- Objects and arrays close correctly.
- There are no comments, undefined, NaN or Infinity values.
- Quotes, backslashes and line breaks inside strings are escaped.
- Each object uses unique property names.
- JSON.parse() processes the document without throwing.
- The structure matches the application's JSON Schema, when one exists.
- The document is formatted only after its syntax is valid.
Validate and format JSON in your browser
Paste your document, detect syntax errors, apply indentation or minify it. The content is processed locally and is not sent to our servers.
Open JSON formatterSources and review
This article was reviewed on July 20, 2026 using the JSON standard, MDN documentation and the official JSON Schema documentation.
- RFC 8259 — The JavaScript Object Notation (JSON) Data Interchange Format
- ECMA-404 — The JSON Data Interchange Syntax
- MDN — JSON.parse()
- MDN — JSON.stringify()
- JSON Schema — Official documentation
General educational content. Compatibility and error messages can vary between parsers, languages and browsers.