JSON Data Types Explained
A comprehensive guide to the six JSON data types: strings, numbers, booleans, null, objects, and arrays. Includes practical examples and common mistakes to avoid when working with JSON data.
JSON (JavaScript Object Notation) is everywhere in modern programming. Whether you're building web applications, working with APIs, or storing configuration data, understanding JSON data types is essential. Let's break down each data type with clear examples that you can try yourself.
What Makes JSON Special
JSON uses a simple syntax that's both human-readable and machine-parseable. Unlike some data formats, JSON supports only six data types, making it straightforward to learn and use consistently across different programming languages.
The Six JSON Data Types
1. String
Strings in JSON must be enclosed in double quotes. They can contain any Unicode character, including special characters that are escaped with backslashes.
{
"name": "Alice Johnson",
"email": "[email protected]",
"message": "Hello, \"world\"!",
"unicode": "Café ☕"
}Common escape sequences include \" for quotes, \\ for backslashes, and \n for newlines.
2. Number
JSON doesn't distinguish between integers and floating-point numbers. All numbers are treated the same way, and you don't need quotes around them.
{
"age": 25,
"height": 5.8,
"temperature": -10.5,
"score": 0,
"scientific": 1.23e-4
}JSON supports positive numbers, negative numbers, decimals, and scientific notation.
3. Boolean
Boolean values are either true or false (lowercase, no quotes). These are perfect for flags and settings.
{
"isActive": true,
"hasPermission": false,
"debugMode": true
}4. Null
The null value represents the absence of data. It's different from an empty string or zero – it explicitly means "no value."
{
"middleName": null,
"spouse": null,
"notes": null
}5. Object
Objects are collections of key-value pairs enclosed in curly braces. Keys must be strings, and values can be any JSON data type.
{
"user": {
"id": 123,
"profile": {
"firstName": "John",
"lastName": "Doe",
"preferences": {
"theme": "dark",
"notifications": true
}
}
}
}Objects can be nested to any depth, making them perfect for representing complex data structures.
6. Array
Arrays are ordered lists of values enclosed in square brackets. Array elements can be any JSON data type, and they don't all need to be the same type.
{
"numbers": [1, 2, 3, 4, 5],
"colors": ["red", "green", "blue"],
"mixed": [42, "hello", true, null],
"users": [
{"name": "Alice", "age": 30},
{"name": "Bob", "age": 25}
]
}Combining Data Types
Real-world JSON often combines multiple data types. Here's a practical example of a user profile that uses all six types:
{
"userId": 12345,
"username": "developer123",
"isActive": true,
"lastLogin": null,
"profile": {
"firstName": "Sarah",
"age": 28,
"skills": ["JavaScript", "Python", "JSON"]
},
"settings": {
"darkMode": true,
"emailNotifications": false
}
}Common Mistakes to Avoid
When working with JSON data types, watch out for these common errors:
- Using single quotes instead of double quotes for strings
- Adding trailing commas after the last element
- Forgetting to quote object keys
- Using
TrueorFalseinstead oftrueorfalse
What's Next
Now that you understand JSON data types, the next step is learning how to work with JSON in your programming language of choice. We'll cover JSON parsing, validation, and manipulation techniques that will help you handle real-world data effectively.