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
| Operator | Description | Example Query and Output | |
|---|---|---|---|
-> | Access a JSONB field, returning a JSONB value. | SELECT '[{"foo":"bar"}]'::JSONB->0->'foo'; "bar"::JSONB | |
->> | Access a JSONB field, returning a string. | SELECT '{"foo":"bar"}'::JSONB->>'foo'; bar::STRING | |
@> | Tests whether the left JSONB field contains the right JSONB field. | SELECT ('{"foo": {"baz": 3}, "bar": 2}'::JSONB@>'{"foo": {"baz":3}}'::JSONB ); true | |
<@ | Tests whether the left JSONB field is contained by the right JSONB field. | SELECT('{"bar":2}'::JSONB<@'{"foo":1, "bar":2}'::JSONB); true | |
#> | Access a JSONB field at the specified path, returning a JSONB value. | SELECT '[{"foo":"bar"}]'::JSONB#>'{0,foo}'; "bar"::JSONB | |
#>> | Access a JSONB field at the specified path, returning a string. | SELECT '[{"foo":"bar"}]'::JSONB#>>'{0,foo}'; bar::STRING | |
? | Does the key or element string exist within the JSONB value? | SELECT('{"foo":1, "bar":2}'::JSONB?'bar'); true | |
?& | Do all the key or element strings exist within the JSONB value? | SELECT('{"foo":1, "bar":2}'::JSONB?&array['foo','bar']); true | |
| `? | ` | Do any of the key or element strings exist within the JSONB value? | SELECT('{"foo":1, "bar":2}'::JSONB?|array['bar']); true |
[ … ] | Access a JSONB key, returning a JSONB value or object. For details, see . | SELECT('{"foo": {"bar":1}}'::JSONB)['foo']['bar']; 1 |
JSONB operators, see .
Functions
| Function | Description |
|---|---|
jsonb\_array\_elements() | Expands a JSONB array to a set of JSONB values. See Map a JSONB array field into rows. |
jsonb\_build\_object(...) | Builds a JSONB object out of a variadic argument list that alternates between keys and values. |
jsonb\_each() | Expands the outermost JSONB object into a set of key-value pairs. See Retrieve key-value pairs from a JSONB field. |
jsonb\_object\_keys() | Returns sorted set of keys in the outermost JSONB object. See Retrieve the distinct keys from a JSONB field. |
jsonb\_pretty() | Returns the given JSONB value as a STRING indented and with newlines. See Retrieve formatted JSONB data. |
jsonb\_set(val: jsonb, path: string[], to: jsonb) | Returns the JSON value pointed to by the variadic arguments. See Update an array element. |
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.

