What is JSON?
JSON (JavaScript Object Notation) is a lightweight data interchange format that has become the de facto standard for data exchange on the web. Originally derived from JavaScript, JSON is now language-independent and supported by virtually every programming language.
JSON's popularity stems from its simplicity. Unlike XML, JSON uses a minimal syntax that's easy to read and write. It represents data as key-value pairs (objects) and ordered lists (arrays), which maps naturally to data structures in most programming languages.
JSON Syntax Rules
Understanding JSON syntax is crucial for avoiding common errors. Here are the fundamental rules:
1. Data Types
JSON supports six data types:
- Strings: Text enclosed in double quotes (
"hello") - Numbers: Integer or floating-point (
42,3.14,-17,2.5e10) - Booleans:
trueorfalse(lowercase only) - Null:
null(lowercase only) - Objects: Collections of key-value pairs (
{"key": "value"}) - Arrays: Ordered lists (
[1, 2, 3])
2. Object Structure
Objects are enclosed in curly braces and contain key-value pairs separated by commas:
{
"name": "John Doe",
"age": 30,
"isActive": true,
"email": null
}
3. Array Structure
Arrays are enclosed in square brackets with values separated by commas:
["apple", "banana", "cherry"]
[1, 2, 3, 4, 5]
[{"id": 1}, {"id": 2}, {"id": 3}]
4. Key Rules
- Keys must be strings (enclosed in double quotes)
- Keys should be unique within an object
- Keys are case-sensitive (
"Name"and"name"are different)
Common JSON Mistakes
These are the most frequent errors developers make with JSON:
1. Trailing Commas
Unlike JavaScript, JSON does not allow trailing commas:
// INVALID - trailing comma
{
"name": "John",
"age": 30, // <- Error!
}
// VALID
{
"name": "John",
"age": 30
}
2. Single Quotes
JSON requires double quotes for strings:
// INVALID
{'name': 'John'}
// VALID
{"name": "John"}
3. Comments
Standard JSON does not support comments:
// INVALID
{
"name": "John", // This is a comment
/* Multi-line comment */
"age": 30
}
Note: Some parsers support JSON5 or JSONC which allow comments, but standard JSON does not.
4. Unquoted Keys
// INVALID
{name: "John"}
// VALID
{"name": "John"}
Formatting Best Practices
1. Use Consistent Indentation
For human-readable JSON, use consistent indentation (typically 2 or 4 spaces). Most formatters default to 2 spaces:
{
"user": {
"id": 123,
"profile": {
"firstName": "John",
"lastName": "Doe"
}
}
}
2. Minify for Production
When transmitting JSON over networks, remove whitespace to reduce payload size:
{"user":{"id":123,"profile":{"firstName":"John","lastName":"Doe"}}}
This reduces size significantly, especially for large datasets.
3. Use Meaningful Key Names
Choose descriptive, consistent key names:
- Use camelCase (
firstName) or snake_case (first_name) consistently - Avoid abbreviations that aren't universally understood
- Be consistent across your entire API
4. Handle Special Characters
Escape special characters in strings properly:
\"for double quotes\\for backslash\nfor newline\tfor tab\uXXXXfor Unicode characters
JSON Schema Validation
For complex applications, consider using JSON Schema to validate your data structure:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"name": {"type": "string", "minLength": 1},
"age": {"type": "integer", "minimum": 0},
"email": {"type": "string", "format": "email"}
},
"required": ["name", "email"]
}
JSON Schema helps catch errors early and documents your data structure.
Working with Large JSON Files
When dealing with large JSON files, consider these strategies:
- Streaming parsers: Process JSON incrementally rather than loading it all into memory
- Pagination: Break large datasets into smaller chunks
- Compression: Use gzip when transmitting large JSON payloads
- Alternative formats: Consider binary formats like Protocol Buffers for very large datasets
Try Our JSON Formatter
Format, validate, and beautify your JSON data instantly with our free online tool.
Open JSON Formatter →