JSON Education & Formatting

Complete JSON Guide

Introduction to JSON

JSON (JavaScript Object Notation) is a lightweight, text-based data format that is language-independent and widely used for data interchange between web applications, APIs, and services.

Why JSON?

JSON Syntax & Data Types

JSON supports six data types that can be used to represent different kinds of values:

1. String

Text values enclosed in double quotes

"name": "John Doe"
"description": "A JSON string value"

2. Number

Integer or floating-point values (no quotes)

"age": 30
"price": 19.99
"temperature": -5

3. Boolean

True or false values (lowercase, no quotes)

"active": true
"isAdmin": false

4. Null

Represents absence of value

"middleName": null

5. Array

Ordered collection of values enclosed in square brackets

"tags": ["javascript", "json", "web"]
"numbers": [1, 2, 3, 4, 5]

6. Object

Unordered collection of key-value pairs enclosed in curly braces

{
  "name": "John",
  "age": 30,
  "address": {
    "street": "123 Main St",
    "city": "New York"
  }
}

Understanding JSON Structure

JSON Objects

Objects contain key-value pairs separated by colons:

{
  "key1": "value1",
  "key2": "value2",
  "key3": 123
}

JSON Arrays

Arrays contain ordered elements of any type:

[
  "string value",
  123,
  true,
  null,
  { "object": "inside array" }
]

Nested Structures

Objects and arrays can be nested to any depth:

{
  "users": [
    {
      "id": 1,
      "name": "Alice",
      "roles": ["admin", "user"]
    },
    {
      "id": 2,
      "name": "Bob",
      "roles": ["user"]
    }
  ]
}

JSON Validation

Validation ensures JSON conforms to the correct syntax and structure. Valid JSON must follow strict rules:

Valid vs Invalid JSON

✓ Valid

{
  "name": "Alice",
  "age": 28,
  "active": true
}

✗ Invalid

{
  name: "Alice",
  'age': 28,
  "active": true,
}

Formatting & Indentation

Proper formatting makes JSON easier to read and debug. The most common indentation sizes are 2 or 4 spaces.

2-Space Indentation

{
  "user": {
    "id": 1,
    "name": "John",
    "email": "john@example.com"
  }
}

4-Space Indentation

{
    "user": {
        "id": 1,
        "name": "John",
        "email": "john@example.com"
    }
}

Minified (No Whitespace)

Compact format used for efficient data transfer:

{"user":{"id":1,"name":"John","email":"john@example.com"}}

Common JSON Errors & Fixes

1. Single Quotes Instead of Double Quotes

❌ Wrong

{'name': 'John'}

✓ Correct

{"name": "John"}

2. Trailing Commas

❌ Wrong

{
  "name": "John",
  "age": 30,
}

✓ Correct

{
  "name": "John",
  "age": 30
}

3. Unquoted Keys

❌ Wrong

{name: "John"}

✓ Correct

{"name": "John"}

4. Comments (Not Allowed)

❌ Wrong

{
  "name": "John", // user name
  "age": 30       /* age in years */
}

ℹ Solution

Use documentation or JSON5. Standard JSON doesn't support comments.

5. Unescaped Special Characters

❌ Wrong

{"path": "C:\Users\John"}

✓ Correct

{"path": "C:\\Users\\John"}

JSON Best Practices

1. Use Consistent Naming Conventions

Choose between camelCase, snake_case, or kebab-case and stick with it throughout your JSON data.

2. Keep Nesting Reasonable

Avoid deeply nested structures (more than 3-4 levels). Flatten when possible for better readability and performance.

3. Use Descriptive Key Names

Avoid abbreviations and single-letter keys. Use clear, self-documenting names like "firstName" instead of "fn".

4. Validate Early and Often

Validate JSON at system boundaries (API inputs, file loads) to catch errors early in the process.

5. Use Standard Data Types

Stick to the six JSON data types. Avoid storing complex types like dates as strings when possible, or use ISO 8601 format.

6. Document Your Schema

Provide documentation or JSON Schema to describe the structure, required fields, and data types of your JSON.

7. Use Null Appropriately

Use null to represent absence of value. Omitting keys entirely is also acceptable depending on your use case.

8. Format for Human Readability

Use indentation and whitespace during development. Use minified JSON only for production data transfer.