JSONB stores JSON (JavaScript Object Notation) data as a binary representation of the JSONB value, which eliminates whitespace, duplicate keys, and key ordering. JSONB supports .
Alias
In CockroachDB,JSON is an alias for JSONB.
In PostgreSQL,
JSONB and JSON are two different data types. In CockroachDB, the JSONB / JSON data type is similar in behavior to the JSONB data type in PostgreSQL.Syntax
The syntax for theJSONB data type follows the format specified in RFC8259. You can express a constant value of type JSONB using an or a string literal type JSONB.
There are six types of JSONB values:
null- Boolean
- String
- Number (i.e., , not the standard
int64) - Array (i.e., an ordered sequence of
JSONBvalues) - Object (i.e., a mapping from strings to
JSONBvalues)
'[{"foo":"bar"}]''{"type": "account creation", "username": "harvestboy93"}''{"first_name": "Ernie", "status": "Looking for treats", "location": "Brooklyn"}''{"prices": [ { "05/01/2022": 100.5 }, { "06/01/2022": 101.5 } ]}'
If duplicate keys are included in the input, only the last value is kept.
Size
The size of aJSONB value is variable, but we recommend that you keep values under 1 MB to ensure satisfactory performance. Above that threshold, and other considerations may cause significant performance degradation.
We strongly recommend adding size limits to all , which includes columns in .
Values exceeding 1 MiB can lead to and cause significant performance degradation or even .
To add a size limit using :
Operators
For the full list of supported
JSONB operators, see .
Functions
For the full list of supported
JSONB functions, see .
Index JSONB data
To a JSONB column you can use a or .
Known limitations
- You cannot use , , and on
JSONBvalues.
Examples
This section shows how to create tables withJSONB columns and use operators and functions to access and update JSONB data. For the full list of operators and functions, see and .
Create a table with a JSONB column
Retrieve formatted JSONB data
To retrieve JSONB data with easier-to-read formatting, use the jsonb_pretty() function. For example, retrieve data from the table you created in the first example:
Retrieve a specific field from JSONB data
To retrieve a specific field from JSONB data, use the -> operator. For example, to retrieve a field from the table you created in Create a table with a JSONB column, run:
->> operator to return JSONB fields as STRING values:
@> operator to filter the values in a field in a JSONB column:
#>> operator with a path to return all first names:
Retrieve the distinct keys from a JSONB field
Retrieve key-value pairs from a JSONB field
Group and order JSONB values
To organize your JSONB field values, use the GROUP BY and ORDER BY clauses with the ->> operator. For example, organize the first_name values from the table you created in the first example:
For this example, we will add a few more records to the existing table. This will help us see clearly how the data is grouped.
->> operator returns STRING and uses string comparison rules to order the data. If you want numeric ordering, cast the resulting data to FLOAT.
Map a JSONB array field into rows
To map a JSONB array field into rows, use the jsonb_array_elements function:
Access nested JSONB fields
To display the commodity prices for May, run:
Update an array element
To update a field value, use thejsonb_set function. For example, to update the price of silver on 06/01/2022 to 90.5, run:
Create a table with a JSONB column and a computed column
In this example, create a table with a JSONB column and a stored computed column:
id is computed as a field from the profile column. Additionally the age column is computed from the profile column data as well.
This example shows how add a stored computed column with a :
Create a table with a JSONB column and a virtual computed column
In this example, create a table with a JSONB column and virtual computed columns:
full_name is computed as a field from the profile column’s data. The first name and last name are concatenated and separated by a single whitespace character using the .
The virtual column birthday is parsed as a TIMESTAMP value from the profile column’s birthdate string value. The is used to parse strings in TIMESTAMP format.
Supported casting and conversion
This section describes how to cast and convertJSONB values.
You can all JSONB values to the following data type:
JSONB values to the following numeric data types:
TIMESTAMP format.
parse_timestamp function to retrieve string representations of timestamp data within JSONB columns in TIMESTAMP format.

