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. Despite its name, JSON is language-independent and used across virtually all programming languages.
JSON is the most common format for API responses, configuration files, and data storage in modern web applications.
JSON Syntax Rules
• Data is in name/value pairs
• Keys must be strings in double quotes
• Data is separated by commas
• Curly braces {} hold objects
• Square brackets [] hold arrays
• No trailing commas allowed
• No comments allowed
• No single quotes (use double quotes only)
JSON Data Types
- String: "Hello World" (always double-quoted)
- Number: 42, 3.14, -17, 1.2e10 (no quotes)
- Boolean: true, false (lowercase, no quotes)
- Null: null (lowercase, no quotes)
- Array: [1, 2, 3] or ["a", "b", "c"]
- Object: {"key": "value", "key2": 123}
Common JSON Errors
❌ {name: "John"} → Keys must be quoted
❌ {'name': 'John'} → Must use double quotes
❌ {"items": [1, 2, 3,]} → No trailing commas
❌ {"name": "John",} → No trailing commas
❌ {"value": undefined} → undefined not allowed
❌ // comment → Comments not allowed
✅ {"name": "John", "age": 30}
Why Format JSON?
- Readability: Indented JSON is much easier to scan and understand.
- Debugging: Find errors in data structure more easily.
- Documentation: Formatted JSON is better for docs and examples.
- Code reviews: Reviewers can understand data changes faster.
Why Minify JSON?
- Smaller file size: Removes whitespace, reducing transmission size.
- Faster APIs: Less data to send over the network.
- Storage efficiency: Less disk space for storing JSON files.
- Production use: Always minify JSON in production APIs.
JSON vs XML
- Size: JSON is typically 30-50% smaller than equivalent XML.
- Parsing: JSON parsing is faster and simpler.
- Readability: JSON is generally easier to read.
- Data types: JSON has native number/boolean types; XML is all strings.
- Use cases: JSON dominates APIs; XML still used in documents, configs, SOAP.
Frequently Asked Questions
Why can't I use single quotes in JSON?
The JSON specification (RFC 8259) strictly requires double quotes for strings and keys. Single quotes are a JavaScript/Python convention but not valid JSON. Always use double quotes.
Can JSON have comments?
Standard JSON does not support comments. If you need comments, use JSON5 or JSONC (JSON with Comments) formats, which some tools support. For configs, consider YAML instead.
What's the maximum size of a JSON file?
JSON itself has no size limit. Practical limits depend on the parser and available memory. Most browsers can handle JSON files of several MB. For very large data, consider streaming parsers.
How do I handle special characters in JSON?
Special characters must be escaped: \" for quotes, \\ for backslash, \n for newline, \t for tab. Unicode characters can be written as \uXXXX (e.g., \u00e9 for é).
Is there a difference between {} and null?
Yes. {} is an empty object (exists but has no properties). null represents the intentional absence of any value. They have different meanings in most applications.
Why does my JSON have "undefined" errors?
undefined is a JavaScript value that doesn't exist in JSON. When serializing objects with undefined values, they're usually omitted. If you need to represent "no value," use null instead.
