What is JSON?
JSON (JavaScript Object Notation) is a lightweight 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 the JavaScript Programming Language Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.
Why use JSON?
- Human-Readable: JSON is easy to read and understand, making it accessible for developers and non-developers alike.
- Lightweight: JSON is a lightweight format, which means it requires less bandwidth and storage compared to other formats like XML.
- Language-Independent: JSON can be used with virtually any programming language, making it a versatile choice for data interchange.
- Structured Data: JSON supports complex data structures, including nested objects and arrays, allowing for rich data representation.
- Widely Supported: JSON is widely supported by web APIs and services, making it a standard choice for data exchange on the web.
Structure and Syntax
1. Primitive Types
JSON supports these primitive data types:
{ "string": "Hello World", // Text data "number": 42, // Integer "float": 3.14159, // Decimal number "boolean": true, // true or false "null": null // Null value }
2. Arrays
Arrays are ordered collections of values:
{ "empty": [], // Empty array "numbers": [1, 2, 3, 4, 5], // Number array "strings": ["red", "green"], // String array "mixed": [1, "hello", true], // Mixed types "nested": [[1, 2], [3, 4]] // Nested arrays }
3. Objects
Objects are collections of key-value pairs:
{ "person": { "name": "John Doe", "age": 30, "address": { "street": "123 Main St", "city": "Boston" } } }
4. Complex Example
Real-world JSON often combines multiple types:
{ "id": 1001, "active": true, "name": "Product X", "price": 99.99, "tags": ["new", "sale"], "stock": { "warehouse": 50, "retail": 10 }, "variations": [ { "color": "red", "size": "M", "available": true }, { "color": "blue", "size": "L", "available": false } ] }
Common JSON Errors
1. Missing or Extra Commas
Commas must separate array elements and object properties, but shouldn't appear after the last item:
{ "name": "John", // Correct "age": 30, // Correct "city": "Boston", // Extra comma - ERROR! }
2. Single Quotes Instead of Double Quotes
JSON requires double quotes for strings:
{ 'name': 'John', // ERROR! Single quotes not allowed "age": 30 }
3. Including Functions or Undefined
JSON doesn't support functions or undefined values:
{ "name": "John", "callback": function() {}, // ERROR! Functions not allowed "status": undefined // ERROR! undefined not allowed }
4. Unquoted Property Names
All object property names must be quoted:
{ name: "John", // ERROR! Missing quotes around property name "age": 30 }
5. Invalid Number Formats
Numbers can't start with multiple zeros or use certain notations:
{ "amount": 01.50, // ERROR! Leading zero "hex": 0xFF, // ERROR! Hex not allowed "scientific": 1.23e5, // Valid scientific notation "infinity": Infinity // ERROR! Infinity not allowed }
6. Comments in JSON
JSON doesn't support comments:
{ // This is a comment ERROR! "name": "John", "age": 30 /* Block comment - ERROR! */ }
7. Unclosed Structures
All arrays and objects must be properly closed:
{ "name": "John", "items": [ "apple", "orange" // ERROR! Missing closing bracket "age": 30