What is JSON?
JSON (JavaScript Object Notation) is a lightweight, text-based data interchange format that's easy for humans to read and write, and easy for machines to parse and generate. Created by Douglas Crockford, JSON has become the de facto standard for data exchange on the web, used extensively in APIs, configuration files, and data storage.
JSON is built on two universal data structures:
- Objects: An unordered collection of key/value pairs enclosed in curly braces
{} - Arrays: An ordered list of values enclosed in square brackets
[]
JSON supports the following data types:
- String: Text enclosed in double quotes, e.g.,
"hello" - Number: Integer or floating point, e.g.,
42,3.14 - Boolean:
trueorfalse - Null:
nullrepresents an empty value - Object: Collection of key/value pairs
- Array: Ordered list of values
Common JSON Errors
JSON syntax is strict, and even small mistakes can make your JSON invalid. Here are the most common errors developers encounter:
❌ Wrong: {"a": 1, "b": 2,}
✓ Correct: {"a": 1, "b": 2}
❌ Wrong: {'name': 'John'}
✓ Correct: {"name": "John"}
❌ Wrong: {name: "John"}
✓ Correct: {"name": "John"}
❌ Wrong: ["apple", "banana",]
✓ Correct: ["apple", "banana"]
❌ Wrong: {"name": "John" // comment}
✓ Correct: {"name": "John"}
❌ Wrong: {"path": "C:\folder"}
✓ Correct: {"path": "C:\\folder"}
JSON vs XML
While both JSON and XML are used for data exchange, JSON has largely replaced XML in modern web development. Here's why:
| Feature | JSON | XML |
|---|---|---|
| Readability | More concise and easier to read | Verbose with opening/closing tags |
| Data Types | Supports strings, numbers, booleans, null, objects, arrays | Everything is a string by default |
| Size | Smaller file size | Larger due to tag overhead |
| Parsing Speed | Faster to parse | Slower to parse |
| Arrays | Native array support | No native array support |
| Namespaces | No namespace support | Supports namespaces |
| Comments | Not supported | Supports comments |
| Use Cases | APIs, web services, configuration | Document markup, enterprise systems |
Bottom line: JSON is the preferred choice for modern web APIs and most data exchange scenarios due to its simplicity, efficiency, and native JavaScript support. XML remains useful for document-centric applications and systems requiring complex validation schemas.