Complete JSON Guide: Master JSON Syntax & Validation
JSON (JavaScript Object Notation) is the most popular data interchange format used in modern web development. This comprehensive guide will teach you everything you need to know about JSON, from basic syntax to advanced validation techniques.
What is JSON?
JSON 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. Despite its name suggesting a connection to JavaScript, JSON is language-independent and is used across virtually all programming languages.
💡 Quick Tip
🚀 Start validating your JSON in real-time as you follow this guide.
JSON Syntax Rules
JSON syntax is derived from JavaScript object notation, but follows strict rules:
- Data is in name/value pairs - separated by commas
- Data is separated by commas - no trailing commas allowed
- Curly braces hold objects - {}
- Square brackets hold arrays - []
- Strings must be in double quotes - "string"
- Numbers can be integers or floating point
- Boolean values are true or false
- null represents empty value
Basic JSON Example
{
"name": "John Doe",
"age": 30,
"isEmployed": true,
"address": {
"street": "123 Main St",
"city": "New York",
"zipCode": "10001"
},
"hobbies": ["reading", "swimming", "coding"],
"spouse": null
}
JSON Data Types
1. String
Strings must be enclosed in double quotes. Single quotes are not valid in JSON.
{
"validString": "Hello World",
"invalidString": 'This will cause an error'
}
2. Number
JSON supports integers and floating-point numbers. No leading zeros allowed (except for 0 itself).
{
"integer": 42,
"float": 3.14159,
"negative": -17,
"scientific": 1.23e-4,
"invalid": 007
}
3. Boolean
Only true
and false
(lowercase) are valid boolean values.
{
"isActive": true,
"isDeleted": false
}
4. null
Represents an empty value. Must be lowercase.
{
"middleName": null,
"invalid": NULL
}
5. Object
A collection of key/value pairs enclosed in curly braces.
{
"user": {
"id": 1,
"profile": {
"firstName": "Jane",
"lastName": "Smith"
}
}
}
6. Array
An ordered list of values enclosed in square brackets.
{
"numbers": [1, 2, 3, 4, 5],
"mixed": ["string", 42, true, null, {"key": "value"}],
"empty": []
}
Common JSON Validation Errors
⚠️ Most Common Mistakes
These errors account for 80% of JSON validation failures:
1. Trailing Commas
// ❌ Invalid - trailing comma
{
"name": "John",
"age": 30,
}
// ✅ Valid
{
"name": "John",
"age": 30
}
2. Single Quotes
// ❌ Invalid - single quotes
{
'name': 'John'
}
// ✅ Valid - double quotes
{
"name": "John"
}
3. Unquoted Keys
// ❌ Invalid - unquoted key
{
name: "John"
}
// ✅ Valid - quoted key
{
"name": "John"
}
4. Comments
// ❌ Invalid - comments not allowed
{
"name": "John", // This is a comment
/* Multi-line comment */
"age": 30
}
// ✅ Valid - no comments
{
"name": "John",
"age": 30
}
JSON Best Practices
1. Use Consistent Naming Conventions
- camelCase - Most common in JavaScript:
"firstName"
- snake_case - Common in Python/Ruby:
"first_name"
- kebab-case - Less common:
"first-name"
2. Keep It Flat When Possible
// ❌ Overly nested
{
"user": {
"profile": {
"personal": {
"name": {
"first": "John"
}
}
}
}
}
// ✅ Flatter structure
{
"user": {
"firstName": "John",
"lastName": "Doe"
}
}
3. Use Arrays for Lists
// ❌ Using objects as arrays
{
"item1": "Apple",
"item2": "Banana",
"item3": "Orange"
}
// ✅ Using proper arrays
{
"items": ["Apple", "Banana", "Orange"]
}
4. Include Metadata
{
"data": {
"users": [...],
"products": [...]
},
"meta": {
"timestamp": "2024-01-15T10:30:00Z",
"version": "1.0",
"totalRecords": 150
}
}
JSON in APIs
RESTful API Response Structure
{
"status": "success",
"data": {
"id": 123,
"name": "Product Name",
"price": 29.99
},
"message": "Product retrieved successfully",
"timestamp": "2024-01-15T10:30:00Z"
}
Error Response Structure
{
"status": "error",
"error": {
"code": "VALIDATION_ERROR",
"message": "Invalid input data",
"details": [
{
"field": "email",
"message": "Email format is invalid"
}
]
},
"timestamp": "2024-01-15T10:30:00Z"
}
JSON Schema Validation
JSON Schema provides a way to validate JSON data structure:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"name": {
"type": "string",
"minLength": 1
},
"age": {
"type": "integer",
"minimum": 0,
"maximum": 150
},
"email": {
"type": "string",
"format": "email"
}
},
"required": ["name", "email"]
}
Performance Tips
- Minimize nesting depth - Deeply nested objects are slower to parse
- Use consistent data types - Avoid mixing types in arrays
- Compress large JSON - Use gzip compression for network transfer
- Stream large datasets - Don't load massive JSON files into memory
- Cache parsed results - Avoid re-parsing the same JSON repeatedly
Tools and Resources
🛠️ Recommended Tools
- JSONly Viewer - Online JSON validator and formatter
- JSONLint - JSON validator
- Postman - API testing with JSON
- VS Code - Built-in JSON support
Conclusion
JSON is an essential skill for modern web development. By following the syntax rules, avoiding common mistakes, and implementing best practices, you'll be able to work effectively with JSON in any project.
Ready to practice? 🚀 Start validating JSON now to validate, format, and explore JSON data with an intuitive tree view interface.