JSON Data Types

Understanding all JSON value types

🔢 JSON Data Types

JSON supports six basic data types. Understanding these types is essential for working with JSON data effectively.


{
  "text": "Hello World",
  "count": 42,
  "active": true,
  "empty": null,
  "list": [1, 2, 3],
  "info": {"key": "value"}
}
                                    

Six JSON Data Types:

String, Number, Boolean, null, Object, Array

All JSON Data Types

""

String

Text wrapped in double quotes

"Hello World"
123

Number

Integer or floating-point

42, 3.14, -17
✓

Boolean

True or false values

true, false
∅

null

Represents empty or no value

null
{}

Object

Collection of key-value pairs

{"name": "John"}
[]

Array

Ordered list of values

[1, 2, 3]

🔹 String Data Type

Strings must be wrapped in double quotes:

{
  "name": "Alice Johnson",
  "email": "[email protected]",
  "message": "Hello, how are you?",
  "empty_string": "",
  "with_quotes": "She said \"Hello!\"",
  "with_newline": "Line 1\nLine 2",
  "unicode": "Café ☕ 🌟"
}

String Rules:

✓ Must use double quotes (not single)

✓ Can contain Unicode characters

✓ Use backslash for escape characters

✓ Can be empty ""

🔹 Number Data Type

JSON numbers can be integers or decimals:

{
  "integer": 42,
  "negative": -17,
  "decimal": 3.14159,
  "scientific": 1.23e-4,
  "zero": 0,
  "large_number": 1234567890
}

Number Rules:

✓ No quotes around numbers

✓ Can be positive or negative

✓ Supports scientific notation

✓ No leading zeros (except for 0.x)

🔹 Boolean and null Types

Simple true/false values and null:

{
  "is_active": true,
  "is_deleted": false,
  "middle_name": null,
  "has_premium": true,
  "last_login": null
}

Important Notes:

  • Boolean: Only true or false (lowercase)
  • null: Represents intentionally empty value
  • No quotes: true, false, null are not strings

🔹 Object Data Type

Objects contain key-value pairs:

{
  "user": {
    "id": 123,
    "profile": {
      "name": "Sarah",
      "age": 28,
      "preferences": {
        "theme": "dark",
        "language": "en",
        "notifications": true
      }
    }
  }
}

Object Features:

✓ Keys must be strings in double quotes

✓ Values can be any JSON data type

✓ Can be nested (objects within objects)

✓ Use commas to separate key-value pairs

🔹 Array Data Type

Arrays hold ordered lists of values:

{
  "numbers": [1, 2, 3, 4, 5],
  "strings": ["apple", "banana", "cherry"],
  "mixed": [1, "hello", true, null],
  "nested_arrays": [[1, 2], [3, 4], [5, 6]],
  "objects_array": [
    {"name": "Alice", "age": 25},
    {"name": "Bob", "age": 30}
  ],
  "empty_array": []
}

Array Features:

✓ Values separated by commas

✓ Can contain any JSON data types

✓ Can mix different data types

✓ Can be empty []

✓ Can contain nested arrays or objects

🔹 Complex Example

Real-world JSON using all data types:

{
  "restaurant": {
    "name": "Bella Vista",
    "rating": 4.5,
    "open": true,
    "chef": null,
    "location": {
      "street": "123 Main St",
      "city": "New York",
      "coordinates": [40.7128, -74.0060]
    },
    "menu": [
      {
        "item": "Margherita Pizza",
        "price": 18.99,
        "vegetarian": true,
        "ingredients": ["tomato", "mozzarella", "basil"]
      },
      {
        "item": "Grilled Salmon",
        "price": 26.50,
        "vegetarian": false,
        "ingredients": ["salmon", "lemon", "herbs"]
      }
    ],
    "reviews_count": 247,
    "last_updated": "2024-01-15"
  }
}

🧠 Test Your Knowledge

Which of these is NOT a valid JSON data type?