Understanding JSON Data Types

In JSON, every value must belong to a valid data type that defines its nature and behavior. These data types form the core structure of any JSON payload, ensuring consistency, readability, and compatibility across platforms. In this tutorial, you will learn how each JSON data type works and how to use them effectively in real-world scenarios.

Valid Data Types in JSON

JSON supports a fixed set of six basic data types used to structure and represent data accurately.

  1. String
  2. Number
  3. Object
  4. Array
  5. Boolean
  6. Null

Invalid JSON Data Types

The following data types are not allowed in JSON and will cause parsing errors if used:
  • Date or datetime objects
  • Functions or executable code
  • Undefined values
  • Infinity and NaN
  • Comments (single-line // or multi-line /* */)
  • RegExp or regular expressions
  • Symbol type (JavaScript-only)

String Data Type

  • A JSON string is any text enclosed in double quotes (" ").
  • It must use double quotes — single quotes are not allowed.
  • Strings can contain letters, digits, spaces, and special characters.
  • Unicode characters (e.g. \u00A9) are fully supported.
  • Escape sequences are used for special characters, such as:
    • \n (newline)
    • \t (tab)
    • \" (double quote inside string)
    • \\ (backslash)
  • Empty strings are valid (e.g. "").
  • Invalid: unescaped characters, or unquoted text (e.g. name: John).

JSON String Examples

Copy
{
  "greeting": "Hello, World!",
  "quote": "He said, \"JSON is easy!\"",
  "path": "C:\\Users\\John\\Documents",
  "newline": "Line1\nLine2",
  "tabbed": "Name:\tJohn",
  "unicode": "Copyright \u00A9 2025",
  "empty": "",
  "numberString": "12345",
  "specialChars": "!@#$%^&*()_+-={}|[]\\:\";'<>?,./"
}

Number Data Type

  • A JSON number can be an integer or a floating-point value.
  • It must not be enclosed in quotes — quoted numbers are treated as strings.
  • Negative numbers are allowed, using a leading minus sign (e.g. -42).
  • Exponential notation is supported (e.g. 6.02e23).
  • Leading zeros are not allowed unless the number is zero (e.g. 012 is invalid).
  • Infinity, NaN, and undefined are not valid JSON numbers.
  • Invalid: numbers with trailing commas or unquoted special values.

JSON Number Examples

Copy
{
  "integer": 100,
  "negative": -42,
  "float": 3.14159,
  "exponential": 6.02e23,
  "zero": 0,
  "largeNegative": -987654321
}

Boolean Data Type

  • A JSON boolean represents a logical value: true or false.
  • It must not be enclosed in quotes — quoted values are treated as strings.
  • Booleans are often used in conditions, flags, or status indicators.
  • Only lowercase true and false are valid — uppercase versions are invalid.
  • Invalid: "true", TRUE, or 1 used as a boolean.

JSON Boolean Examples

Copy
{
  "isActive": true,
  "hasAccess": false,
  "isComplete": true
}

Null Data Type

  • null is a special JSON value representing an empty or non-existent value.
  • It must be written in lowercase without quotes — "null" is a string, not a null value.
  • Invalid: NULL, nil, undefined, or "null".

JSON Null Example

Copy
{
  "middleName": null,
  "error": null,
  "optionalData": null
}

Object Data Type

  • A JSON object is an unordered collection of key/value pairs.
  • Each key must be a string enclosed in double quotes.
  • Values can be any valid JSON data type (string, number, object, array, boolean, or null).
  • Objects are wrapped in curly braces { }.
  • Multiple objects are separated by commas ,.
  • Trailing commas are not allowed.

JSON Object Example

Copy
{
  "book1": {
    "title": "JSON Guide",
    "pages": 120,
    "available": true
  },
  "book2": {
    "title": "XML Handbook",
    "pages": 200,
    "available": false
  }
}

Array Data Type

  • A JSON array is an ordered list of values enclosed in square brackets [ ].
  • Values can be of any valid JSON data type: string, number, object, array, boolean, or null.
  • Elements are separated by commas ,.
  • Arrays can contain mixed data types.
  • Nested arrays are allowed.
  • Trailing commas are not allowed after the last element.
  • Invalid: missing brackets, extra commas, or non-JSON types inside the array.

JSON Array Example

Copy
{
  "languages": ["JavaScript", "Python", "Go"],
  "numbers": [1, 2.5, -3, 0],
  "mixed": ["yes", false, null, { "id": 1 }],
  "nested": [[1, 2], ["a", "b"]]
}

Comparing JSON Data and JSON Schema

While JSON carries actual data, JSON Schema defines the rules and structure that data must follow. Both serve different roles in data validation and design.

Difference Between JSON Data and JSON Schema

  • Purpose: JSON data contains actual values; JSON Schema describes rules those values must follow.
  • Content vs. Metadata: JSON data is your payload; JSON Schema is a separate document with keywords like type and properties.
  • Validation: Raw JSON won’t enforce constraints; JSON Schema enables automated checks (e.g., minimum, pattern).
  • Extensibility: JSON data can’t include schema keywords; JSON Schema can reference other schemas via $ref and define defaults.

JSON Data Sample

Copy
{
  "name": "Alice",
  "age": 30,
  "email": "alice@example.com"
}

JSON Schema Sample

Copy
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "name":  { "type": "string" },
    "age":   { "type": "integer", "minimum": 0 },
    "email": { "type": "string", "format": "email" }
  },
  "required": ["name", "email"],
  "additionalProperties": false
}