JSON Data Types Explained

Learn the six JSON data types: string, number, boolean, null, object, and array. Understand their syntax rules, constraints, and how they map to programming languages.

Concept

Detailed Explanation

JSON supports exactly six data types, divided into two categories: four primitive types (string, number, boolean, null) and two structured types (object, array). Every valid JSON value must be one of these types. There are no additional types for dates, binary data, undefined, or custom types.

String: A sequence of zero or more Unicode characters wrapped in double quotes. Single quotes are not allowed. Strings support escape sequences like \n, \t, and \uXXXX. Example: "Hello, world!".

Number: A numeric value that can be an integer or a floating-point decimal. JSON numbers do not support leading zeros (except for 0 itself), hexadecimal, octal, Infinity, NaN, or a leading plus sign. Valid examples: 42, -3.14, 2.5e10. Invalid examples: 0x1F, +5, .5 (must be 0.5), NaN.

Boolean: One of two literal values: true or false (lowercase only). True, TRUE, False, and FALSE are all invalid. Booleans are not quoted — "true" is a string, not a boolean.

Null: The literal value null (lowercase only) represents the intentional absence of a value. It is not the same as an empty string "", the number 0, or the boolean false. JSON has no undefined type — if a value does not exist, omit the key entirely rather than setting it to undefined.

Object: An unordered collection of key-value pairs enclosed in curly braces {}. Keys must be double-quoted strings. Values can be any JSON type, including nested objects. Example: {"name": "Alice", "age": 30}. Duplicate keys are technically allowed by the spec but strongly discouraged, as behavior varies across parsers.

Array: An ordered sequence of values enclosed in square brackets []. Elements can be any JSON type, and different types can be mixed within the same array. Example: [1, "two", true, null].

Common mistakes developers make:

Developers coming from JavaScript often try to use undefined, Date objects, regular expressions, or functions in JSON. None of these are valid JSON types. Dates must be serialized as ISO 8601 strings or Unix timestamps. Another mistake is using unquoted string values or relying on implicit type coercion. In JSON, 42 is a number and "42" is a string — there is no automatic conversion.

Best practices:

Be explicit about types in your data model. Document whether a field uses a number or a string representation. Use null consistently to represent missing values rather than empty strings or sentinel values like -1. When designing JSON schemas, stick to the simplest type that accurately represents the data.

Use Case

Designing a JSON API response schema and choosing the correct JSON data types for each field, such as using numbers for timestamps and strings for identifiers.

Try It — JSON Formatter

Open full tool