DevToolbox
All guidesDevToolbox guide

How to Format and Validate JSON

A practical, comprehensive guide to structured JSON data, syntax validation, common parsing errors, and best practices for web development.

What is JSON and Why Is It Ubiquitous?

JavaScript Object Notation (JSON) is a lightweight, language-agnostic text format defined by RFC 8259 and ECMA-404. Originating in early JavaScript specifications, JSON quickly superseded XML as the dominant format for data interchange across web applications, REST APIs, microservices, and modern document databases (such as MongoDB and PostgreSQL JSONB columns).

JSON’s widespread adoption stems from its simplicity: it is easily readable by humans, straightforward for computer parsers to tokenize, and maps natively into data structures across virtually every programming language (arrays, dictionaries, maps, and objects).

JSON Syntax Specification: The Core Rules

Unlike loose scripting languages, JSON enforce strict syntax constraints. Violating any of the following constraints will cause parsers to reject the entire document:

  • Double Quotes Exclusively: All property names and string values must be enclosed in ASCII double quotation marks ("). Single quotes (') or backticks (`) are strictly illegal.
  • No Trailing Commas: Commas are delimiters, not terminators. Placing a comma after the final key in an object or the final item in an array (e.g., [1, 2, 3,]) violates the grammar.
  • Restricted Value Types: JSON accepts only six data types:
    1. String: Unicode characters wrapped in double quotes.
    2. Number: Integer or floating-point (scientific notation allowed; no NaN or Infinity).
    3. Boolean: Literal true or false (case-sensitive).
    4. Null: Literal null.
    5. Object: Unordered collection of key/value pairs enclosed in curly braces .
    6. Array: Ordered list of values enclosed in square brackets [].
  • No Comments: Standard JSON does not permit comments (neither // nor /* */). If comments are needed, developers must use extended formats such as JSON5 or YAML.

Minification vs. Beautification (Formatting)

When APIs serialize JSON for wire transmission across networks, they minify the payload by removing all unnecessary whitespace characters, tabs, and newlines. While this reduces bandwidth consumption, it turns complex data into an illegible wall of text.

Beautification introduces 2-space or 4-space indentation and line breaks matching the depth of each nested array or object. This allows developers to inspect nested structures, verify relationship hierarchy, and review changes during pull requests.

Troubleshooting Common JSON Syntax Errors

  • Unexpected token: Check immediately before the reported character position. This frequently denotes a missing comma between properties or an extra trailing comma.
  • Unescaped Quotes Inside Strings: If your string contains quotation marks, escape them with a backslash (e.g., "description": "A \"quoted\" word").
  • Copy-Paste Smart Quotes: Text copied from rich text editors or chat apps often converts straight quotes to slanted "smart" quotes (“ ”), which JSON parsers reject.

Frequently Asked Questions About JSON

Can JSON have comments?

No. Standard JSON (RFC 8259) deliberately excludes comments to prevent people from using parsing directives or annotations that break interoperability across languages. Douglas Crockford removed comments from early specs so that parsers would remain lightweight and deterministic. If your application requires comments in configuration files, consider adopting YAML, JSON5, or TOML instead.

What is the difference between JSON and JavaScript objects?

While JSON syntax is derived from JavaScript object literal notation, they are distinct standards. In JavaScript, keys do not require quotes unless they contain special characters, single quotes are valid for strings, and values can be functions, symbols, or undefined. In JSON, keys and strings must be double-quoted, trailing commas are strictly invalid, and functions or undefined are completely forbidden.

How do I handle very large numbers in JSON?

JSON numbers are arbitrary-precision decimal numbers according to the specification. However, most JavaScript parsers decode numbers into 64-bit IEEE 754 floating-point values (floats). Integers greater than Number.MAX_SAFE_INTEGER (253 - 1, or 9,007,199,254,740,991) will lose precision. When exchanging 64-bit database IDs (such as Twitter snowflakes or BigInt integers), always serialize them as quoted strings.

JSON Schema: Validating Data Structure

While basic syntax validation confirms whether a string can be parsed, JSON Schema provides a vocabulary that allows you to annotate and validate the structure, constraints, and data types of JSON documents. JSON Schema can enforce:

  • Required object fields and permissible property names.
  • Minimum and maximum numerical boundaries.
  • String formats such as regular expression patterns, email addresses, and date-times.
  • Array length constraints and element type consistency.

Try the related tool

JSON Formatter & Validator

More guides