JSON: The Complete Guide for Developers

Everything you need to know about JSON - syntax, data types, and best practices.

basicsdataweb

What is JSON?

JSON (JavaScript Object Notation) is a lightweight data interchange format. It's easy for humans to read and write, and easy for machines to parse and generate.

JSON Syntax

{
"name": "John Doe",
"age": 30,
"isActive": true,
"email": null,
"hobbies": ["reading", "coding", "gaming"],
"address": {
"street": "123 Main St",
"city": "New York"
}
}

Data Types

TypeExampleNotes
String"hello"Must use double quotes
Number42, 3.14No leading zeros
Booleantrue, falseLowercase only
NullnullLowercase only
Array[1, 2, 3]Ordered list
Object{"key": "value"}Unordered key-value pairs

Common Mistakes

// ❌ Wrong: Single quotes
{'name': 'John'}

// ✅ Correct: Double quotes
{"name": "John"}

// ❌ Wrong: Trailing comma
{"name": "John",}

// ✅ Correct: No trailing comma
{"name": "John"}

// ❌ Wrong: Comments
{
"name": "John" // this is a comment
}

// ✅ JSON doesn't support comments
{"name": "John"}

JSON vs JavaScript Objects

// JavaScript object (more flexible)
const jsObj = {
name: 'John', // Unquoted keys OK
'full-name': 'John Doe', // Quoted keys OK
greet() { }, // Methods allowed
};

// JSON (stricter)
const json = {
"name": "John", // Keys must be quoted
"full-name": "John Doe" // Always double quotes
// No methods, no functions
};

Frequently Asked Questions

Common questions about this topic

JSON's strict syntax doesn't allow trailing commas for simplicity and unambiguous parsing. While JavaScript objects allow them, JSON was designed as a minimal data format. Many JSON parsers would fail on trailing commas. Use a linter or formatter to catch this.

Standard JSON doesn't support comments. Workarounds: 1) Use JSONC (JSON with Comments) for config files - many tools support it, 2) Add a '_comment' key, 3) Use YAML instead if comments are important. Don't strip comments with preprocessing - it's fragile.

JSON has no theoretical size limit - it depends on your parser and memory. JavaScript can handle tens of megabytes. For very large data: stream parse instead of loading entire file, use newline-delimited JSON (NDJSON), or consider binary formats like Protocol Buffers.