Developer Guide · September 10, 2026
JSON Formatting and Validation — A Complete Guide for Developers
A practical guide to formatting, validating and debugging JSON data, covering syntax rules, common errors, nested structures and browser-based tools for everyday development work.
JSON (JavaScript Object Notation) is the standard data interchange format for web APIs, configuration files, database exports and inter-service communication. Despite its simplicity, JSON syntax errors are among the most common debugging tasks developers face daily.
This guide covers everything you need to work with JSON effectively: syntax rules, formatting best practices, common error patterns and the tools that make the process faster.
JSON syntax fundamentals
JSON supports six data types: strings, numbers, booleans, null, objects and arrays. Every valid JSON document is either an object (wrapped in curly braces) or an array (wrapped in square brackets).
Strings
JSON strings must use double quotes. Single quotes, backticks and unquoted strings are not valid JSON, even though they work in JavaScript:
{ "name": "Lumarc Studio" }
Common mistake: using single quotes ('name') or unquoted keys (name: "value") and expecting JSON parsers to accept them.
Numbers
JSON numbers can be integers or floating-point. They cannot have leading zeros, trailing commas or use special values like NaN or Infinity:
{ "count": 42, "ratio": 3.14, "offset": -7 }
Booleans and null
Boolean values are lowercase true and false. Null is lowercase null. Capitalised variants (True, False, None) are not valid JSON:
{ "active": true, "archived": false, "deletedAt": null }
Objects
JSON objects are unordered collections of key-value pairs. Keys must be strings. Values can be any JSON type, including nested objects:
{
"studio": {
"name": "Lumarc Studio",
"products": 10,
"openSource": true
}
}
Arrays
JSON arrays are ordered lists of values. Array elements can be mixed types, though homogeneous arrays are more common in practice:
{
"tools": ["JSON Formatter", "Base64 Encoder", "UUID Generator"]
}
Common JSON errors and how to fix them
Trailing commas
The most common JSON syntax error is a trailing comma after the last element in an object or array. JavaScript allows this, but JSON does not:
// Invalid — trailing comma
{ "name": "DevTools", "version": "1.0", }
// Valid
{ "name": "DevTools", "version": "1.0" }
Missing or extra brackets
Mismatched brackets are easy to introduce in deeply nested structures. A reliable approach is to format the JSON with proper indentation, which makes bracket mismatches visually obvious.
Unescaped special characters in strings
JSON strings cannot contain literal newlines, tabs or backslashes without escaping. Common escape sequences include \" (double quote), \\ (backslash), \n (newline) and \t (tab).
Comments
JSON does not support comments. If you need comments in configuration files, consider using JSON5, JSONC (JSON with Comments, supported by VS Code) or YAML instead.
Formatting JSON for readability
Minified JSON is compact and efficient for transmission, but difficult to read and debug. Formatting (also called “beautifying” or “pretty-printing”) adds consistent indentation and line breaks.
Standard formatting uses 2-space or 4-space indentation. Most formatters also sort keys alphabetically as an option, which helps with comparing two JSON documents.
When to use minified JSON
- API responses in production (smaller payload size)
- Configuration stored in environment variables
- Data embedded in HTML attributes
When to use formatted JSON
- Debugging API responses
- Reading configuration files
- Comparing two JSON documents
- Code review and documentation
- Logging for human inspection
Validating JSON
Validation checks whether a JSON document is syntactically correct and, optionally, whether it conforms to an expected structure.
Syntax validation
The simplest validation is checking whether the JSON parses without errors. In JavaScript, you can use a try-catch around JSON.parse():
try {
const data = JSON.parse(input);
console.log("Valid JSON");
} catch (error) {
console.error("Invalid JSON:", error.message);
}
The limitation of JSON.parse() error messages is that they often lack precise location information. They might say “Unexpected token” without telling you which line or column the error occurs on.
Schema validation
For structured validation, JSON Schema defines the expected shape of your data — required fields, value types, string patterns, numeric ranges and nested object structures. This is useful for validating API request bodies, configuration files and data imports.
Tools for working with JSON
Several tools make JSON work faster:
- JSON Formatter — format and validate JSON with clear error messages showing line and column numbers
- JSON Validator — check whether JSON is valid and see detailed syntax errors
- JSON Minifier — remove whitespace to create compact payloads
- JSON Tree Viewer — visualise nested JSON as an expandable tree
- JSON Compare — compare two JSON documents and list differences by path
- JSON to TypeScript — generate TypeScript interfaces from JSON samples
All of these tools process data locally in your browser. Nothing is uploaded to a server.
Best practices for production JSON
-
Validate early. Validate JSON at the point of entry — API boundaries, file imports and user input — rather than trusting that upstream data is well-formed.
-
Use consistent formatting. Pick 2-space or 4-space indentation for your project and enforce it with linting tools.
-
Prefer explicit types. Use
nullfor absent values rather than empty strings or zero. This makes the distinction between “no value” and “empty value” clear to consumers. -
Document your schema. For any JSON structure that is shared between systems, maintain a JSON Schema definition that serves as both documentation and validation logic.
-
Handle encoding carefully. Ensure strings are valid UTF-8 and properly escape special characters. This is particularly important for JSON that includes user-generated content.
JSON is simple enough to learn in an afternoon and complex enough to cause subtle bugs in production. Having reliable tools and consistent practices makes the difference between productive development and hours of debugging malformed payloads.