What Is JSON? The Complete Guide to JavaScript Object Notation
A comprehensive guide to JSON (JavaScript Object Notation). Learn JSON syntax, data types, objects, arrays, practical API examples, and standard serialization practices.
Introduction to JSON
JSON (JavaScript Object Notation) is a lightweight, text-based, language-independent data interchange format standardized under RFC 8259 and ECMA-404. Originally popularized in the early 2000s as a simpler alternative to XML, JSON has become the universal standard for web API payloads, microservice messaging, and application configuration.
Core JSON Data Types
JSON supports six fundamental data types:
- String: A sequence of Unicode characters wrapped in double quotes (e.g.
"ToolNest"). - Number: Double-precision floating-point numbers, including integers and decimals (e.g.
42,3.14159,-10,1.5e3). Octal and hexadecimal literals are forbidden. - Boolean: Literal
trueorfalse(case-sensitive, lowercase only). - Null: Literal
nullrepresenting an empty or non-existent value. - Object: An unordered collection of zero or more key/value pairs enclosed in curly braces
{}. Keys must be strings wrapped in double quotes. - Array: An ordered sequence of zero or more values enclosed in square brackets
[].
Valid JSON Structure Example
Here is an example representing a user profile and application settings:
{
"userId": 10482,
"username": "sarah_dev",
"email": "sarah@example.com",
"isActive": true,
"profile": {
"title": "Senior Cloud Architect",
"department": "Engineering"
},
"roles": ["admin", "developer", "reviewer"],
"lastLogin": null
}
Why JSON Dominates Modern Web APIs
JSON became the default choice for modern web architectures for several decisive reasons:
- Human Readability: JSON mirrors the key-value map and list structures native to modern programming languages, making payloads straightforward to inspect and debug.
- Native Browser Support: Web browsers parse and serialize JSON natively at C++ engine speeds using
JSON.parse()andJSON.stringify(). - Compact Overhead: Unlike XML, JSON does not require verbose closing tags, significantly reducing bandwidth consumption across mobile and IoT networks.
Common Syntax Mistakes to Avoid
- Single Quotes: Using single quotes like
'name': 'value'is invalid in JSON. Double quotes are mandatory. - Trailing Commas: Adding a comma after the final item in an object or array (e.g.
{"a": 1,}) violates RFC 8259. - Comments: Standard JSON does not permit comments (
//or/* */). - Unquoted Keys: Keys must always be enclosed in double quotes (e.g.
"id": 1, notid: 1).