JSON Data Types Explained

JSON supports exactly six data types, making it remarkably simple compared to full programming languages. Yet these six types are sufficient to represent virtually any data structure.

Strings

Strings in JSON represent text data and are the most commonly used primitive type. They're enclosed in double quotes and can contain any Unicode characters. Certain characters have special meaning and must be escaped with a backslash. Whitespace characters have dedicated escape sequences: \n for newline, \r for carriage return, \t for tab. Any Unicode character can be represented using \uXXXX notation. Strings have no theoretical maximum length, but practical limits depend on the parser. Common uses include names, descriptions, URLs, and dates in ISO 8601 format.

Numbers

JSON numbers represent numeric values and can be integers or decimal numbers. Unlike strings, numbers are not enclosed in quotes. Scientific notation is supported for very large or very small numbers. JSON numbers have several restrictions: no leading zeros, no positive sign prefix, no hexadecimal. Special IEEE floating-point values NaN and Infinity are not supported. Precision is not guaranteed for very large integers. JavaScript safely represents integers only up to 2^53 - 1. For larger values, consider storing them as strings.

Booleans and Null

Booleans and null are the simplest JSON types. Boolean values are written as lowercase true or false without quotes. Null represents the intentional absence of any value, written as lowercase null. Null is distinct from missing keys and from empty values. An empty string "" or empty array [] are values, not null. Be consistent in how your application interprets null versus missing keys.

Objects and Arrays

Objects and arrays are the composite types that give JSON its power. Objects model entities with named properties. Arrays model collections where order matters. Nesting enables rich data modeling. Objects can contain arrays, arrays can contain objects, and both can be nested to any depth. When modeling data, think about how it will be accessed. For large collections, consider pagination. Relationships can be modeled by embedding or by reference.

Vyzkoušet nástroj

JSON Formátovač

JSON Formátovač

Související články