正在加载,请稍候…

How to Format and Validate JSON: A Developer's Guide

Learn to format, validate, and debug JSON with our free online tool. Understand common JSON syntax errors, encoding pitfalls, and best practices for developers.

What Is JSON?

JSON (JavaScript Object Notation) is a lightweight, human-readable data interchange format. Derived from JavaScript object literal syntax, JSON has become the universal standard for data exchange on the web, used in REST APIs, configuration files, and data storage.

JSON supports six data types: strings, numbers, booleans (true/false), null, arrays, and objects. Its simplicity and readability have made it the dominant alternative to XML.

JSON Syntax Rules

JSON has strict syntax requirements that differ from JavaScript:

  1. Keys must be strings in double quotes: "key": "value" (not key: "value")
  2. Strings use double quotes only - no single quotes
  3. No trailing commas: {"a": 1, "b": 2} is valid but {"a": 1, "b": 2,} is not
  4. No comments: JSON does not support // or /* */ comments
  5. Numbers cannot have leading zeros: 100 is valid, 0100 is not
  6. Special values: true, false, null (lowercase)

Common JSON Errors

Syntax Error: Unexpected token

Usually means a trailing comma, missing comma, or unquoted key:

Bad:  {"name": "John", "age": 30,}  <- trailing comma
Good: {"name": "John", "age": 30}

Bad:  {name: "John"}  <- unquoted key
Good: {"name": "John"}

Unexpected control character

Unescaped special characters inside strings:

Bad:  {"path": "C:\Users\name"}    <- backslash not escaped
Good: {"path": "C:\\Users\\name"}  <- escaped backslash

Bad:  {"text": "line 1
line 2"}                           <- unescaped newline
Good: {"text": "line 1\nline 2"}   <- escaped newline

Unclosed bracket or brace

Missing closing } or ]:

Bad:  [{"id": 1}, {"id": 2]  <- missing }
Good: [{"id": 1}, {"id": 2}]

JSON Formatting Levels

Minified JSON (single line, no whitespace):

{"users":[{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}]}

Best for: API responses, data transmission (smaller size).

Pretty-printed JSON (indented, human-readable):

{
  "users": [
    {
      "id": 1,
      "name": "Alice"
    },
    {
      "id": 2,
      "name": "Bob"
    }
  ]
}

Best for: Configuration files, debugging, documentation.

JSON in Practice

Parsing JSON in JavaScript

// Parse (string to object)
const data = JSON.parse('{"name": "Alice", "age": 30}');
console.log(data.name);  // "Alice"

// Stringify (object to string)
const json = JSON.stringify({ name: "Alice", age: 30 });
// '{"name":"Alice","age":30}'

// Pretty print (2-space indent)
const pretty = JSON.stringify(data, null, 2);

JSON Schema Validation

JSON Schema defines the expected structure, types, and constraints of JSON data:

{
  "type": "object",
  "properties": {
    "name": { "type": "string", "minLength": 1 },
    "age": { "type": "number", "minimum": 0 }
  },
  "required": ["name"]
}

Handling Large JSON Files

For large JSON files (>100MB), streaming parsers avoid loading the entire file into memory:

  • Node.js: stream-json package
  • Python: ijson package

JSON vs Other Formats

Format Comments Binary Schema Use Case
JSON No No JSON Schema Web APIs, config
YAML Yes No Yes Config files, DevOps
TOML Yes No Limited Application config
XML Yes No XSD Enterprise, docs
MessagePack N/A Yes No Performance-critical
Protocol Buffers N/A Yes Yes High-performance APIs

Using This Tool

Paste any JSON text to instantly validate it and detect errors with specific line/column numbers. The tool pretty-prints valid JSON with configurable indentation (2 spaces, 4 spaces, or tabs) and highlights any syntax errors.

-> Try the JSON Formatter & Validator