JSON Data Types

JSON supports six basic data types that can be used to structure your data effectively. Understanding these types is crucial for working with JSON files.

Strings

Strings in JSON must be wrapped in double quotes. They can contain any Unicode character, including escaped characters.

{
    "name": "John Doe",
    "message": "Hello, World!",
    "unicode": "こんにちは",
    "escaped": "Line 1\nLine 2\tTabbed"
}

Escape Sequences

  • \" - Double quote
  • \\ - Backslash
  • \/ - Forward slash
  • \n - Newline
  • \r - Carriage return
  • \t - Tab
  • \uXXXX - Unicode character

Numbers

JSON supports both integers and floating-point numbers. Numbers can be positive or negative, and can use exponential notation.

{
    "age": 30,
    "price": 19.99,
    "negative": -42,
    "decimal": 0.5,
    "exponential": 1.5e10,
    "scientific": 6.022e23
}

Number Rules

  • No leading zeros (except for decimals like 0.5)
  • Can use exponential notation (e or E)
  • No NaN or Infinity (use null instead)
  • No hexadecimal or octal notation

Objects

Objects are unordered collections of key-value pairs, enclosed in curly braces. Keys must be strings, and values can be any JSON type.

{
    "person": {
        "firstName": "Jane",
        "lastName": "Smith",
        "age": 28,
        "address": {
            "street": "123 Main St",
            "city": "Boston",
            "zipCode": "02101"
        }
    }
}

Object Characteristics

  • Keys must be unique within an object
  • Keys must be strings (enclosed in double quotes)
  • Order is not guaranteed (though most implementations preserve it)
  • Can be nested to any depth

Arrays

Arrays are ordered lists of values, enclosed in square brackets. Values can be of any type, including mixed types.

{
    "numbers": [1, 2, 3, 4, 5],
    "fruits": ["apple", "banana", "orange"],
    "mixed": [42, "text", true, null, {"key": "value"}],
    "nested": [[1, 2], [3, 4], [5, 6]],
    "empty": []
}

Array Features

  • Zero-indexed (first element is at index 0)
  • Order is preserved
  • Can contain different data types
  • Can be nested
  • Can be empty

Booleans

JSON supports two boolean values: true and false (lowercase only).

{
    "isActive": true,
    "hasAccess": false,
    "verified": true,
    "deleted": false
}

Important Notes

  • Must be lowercase (not True or FALSE)
  • Not wrapped in quotes (that would make them strings)
  • Commonly used for flags and conditions

Null

The null value represents the intentional absence of any value. It must be lowercase.

{
    "middleName": null,
    "spouse": null,
    "previousAddress": null
}

When to Use Null

  • To represent a missing or unknown value
  • Different from an empty string ("") or zero (0)
  • Different from undefined (which doesn't exist in JSON)
  • Useful for optional fields

Data Type Comparison

Type Example Use Case
String "Hello" Text data, names, descriptions
Number 42, 3.14 Quantities, measurements, IDs
Object {"key": "value"} Structured data, records
Array [1, 2, 3] Lists, collections
Boolean true, false Flags, conditions
Null null Missing or unknown values

Common Mistakes

  • Single quotes: JSON requires double quotes for strings
  • Trailing commas: Not allowed after the last item
  • Unquoted keys: All object keys must be in quotes
  • Comments: JSON doesn't support comments
  • Undefined: Use null instead
  • Date objects: Must be stored as strings or numbers
  • Functions: Not supported in JSON

Best Practices

  • Use meaningful key names
  • Keep consistent naming conventions (camelCase or snake_case)
  • Use appropriate types (don't store numbers as strings)
  • Consider using null for optional fields
  • Validate data types when parsing JSON
  • Use arrays for ordered collections
  • Use objects for structured data