What is JSON?

JSON (JavaScript Object Notation) is a lightweight, text-based data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. It is based on a subset of JavaScript syntax but is language-independent.

Quick Facts

Full NameJavaScript Object Notation
Created2001 by Douglas Crockford
SpecificationOfficial Specification

How It Works

JSON uses a simple structure of key-value pairs and ordered lists to represent data. Objects are enclosed in curly braces {} with comma-separated key-value pairs, where keys are strings in double quotes. Arrays are enclosed in square brackets [] with comma-separated values. JSON supports six data types: strings, numbers, booleans (true/false), null, objects, and arrays. Unlike XML, JSON has no closing tags, making it more compact and faster to parse. Several JSON variants have emerged to address limitations of strict JSON. JSON5 adds support for comments, trailing commas, and unquoted keys. JSONC (JSON with Comments) is used by VS Code and TypeScript configurations. JSON Lines (JSONL) stores one JSON object per line for streaming and log processing.

Key Characteristics

  • Human-readable text format with minimal syntax
  • Language-independent but uses conventions familiar to C-family languages
  • Supports nested data structures (objects within objects, arrays within arrays)
  • No support for comments in the official specification
  • Strict syntax rules - keys must be double-quoted strings
  • Smaller file size compared to XML for equivalent data

Common Use Cases

  1. API responses and requests in web services (REST APIs)
  2. Configuration files for applications and tools
  3. Data storage in NoSQL databases like MongoDB
  4. Data exchange between client and server in web applications
  5. Package management files (package.json, composer.json)

Example

loading...
Loading code...

Frequently Asked Questions

What is the difference between JSON and JavaScript objects?

While JSON syntax is derived from JavaScript object notation, they are different. JSON is a text-based data format with strict rules: keys must be double-quoted strings, and values can only be strings, numbers, booleans, null, arrays, or objects. JavaScript objects are runtime data structures that can include functions, undefined values, and use unquoted keys.

Can JSON contain comments?

No, the official JSON specification does not support comments. However, variants like JSON5 and JSONC (JSON with Comments) allow comments. If you need comments in configuration files, consider using YAML or JSON5, or strip comments before parsing.

Why is JSON preferred over XML for APIs?

JSON is preferred for several reasons: it's more compact (no closing tags), easier to parse in JavaScript and most languages, has simpler syntax, and results in smaller payload sizes. JSON also maps directly to native data structures in most programming languages.

How do I validate JSON data?

You can validate JSON syntax using online tools like JSON formatters, or programmatically using JSON.parse() which throws an error for invalid JSON. For validating data structure and types, use JSON Schema which defines the expected format, required fields, and data types.

What are common JSON parsing errors and how to fix them?

Common errors include: trailing commas (remove them), single quotes instead of double quotes (use double quotes), unquoted keys (quote all keys), special characters not escaped (use backslash escaping), and missing commas between elements. Use a JSON validator to identify the exact error location.

Related Tools

Related Terms

Related Articles