JSON and Arrays
This page covers the json and jsonb types, their operators and functions, and SQL arrays. Type names, OIDs and assignment rules are on Data types; GIN indexing of jsonb and array columns is on GIN indexes.
How JSON values are held
A json or jsonb column cell is text. On write, jsonb is re-rendered in PostgreSQL's jsonb output form by pg_jsonb_text (duplicate keys collapsed to the last, keys ordered shortest first then bytewise, one space after , and :); json keeps the text as written. Both must parse, or the write fails with 22P02. (source: src/query/engine.rs, coerce_assign_value_inner; src/query/json_ops.rs, pg_jsonb_text)
During evaluation a JSON value is a Value::Json holding a serde_json::Value. The serde_json crate is built with the preserve_order feature, so object keys keep insertion order in memory; the PostgreSQL key order is applied when the value is rendered as jsonb text. Text cells are parsed on demand, and -> and ->> cache the parse of each distinct document text (parse_json_text_cached). (source: Cargo.toml, serde_json; src/query/engine.rs, jsonb_get_field, parse_json_text_cached)
json and jsonb share one implementation. Operators and functions named json_* and jsonb_* behave the same; the difference is only the OID reported and the text form stored. (source: src/query/engine.rs, evaluate_row_expr JSON arms)
The status note on JSON result gaps records the remaining differences from PostgreSQL found by the diff corpus, which now passes 276 of 279 queries. (source: docs/status/2026-11-30-pg-json-result-gaps.md)
JSON operators
| Operator | Right operand | Result | Behaviour |
|---|---|---|---|
j -> 'key' |
text | json | Object field; NULL if absent |
j -> n |
integer | json | Array element, 0-based |
j ->> 'key', j ->> n
|
text or integer | text | As ->; strings unquoted, nested values in jsonb text form, JSON null becomes SQL NULL |
j #> '{a,b,1}' |
text array | json | Walk a path of keys and 0-based indexes |
j #>> '{a,b}' |
text array | text | As #>, as text |
j ? 'k' |
text | bool | Top-level key exists (or string element, for arrays) |
j ?\| array['a','b'] |
text array | bool | Any key exists |
j ?& array['a','b'] |
text array | bool | All keys exist |
a @> b |
jsonb | bool |
a contains b (recursive structural containment) |
a <@ b |
jsonb | bool |
a is contained in b
|
a \|\| b |
jsonb | jsonb | Objects merged (right wins); arrays concatenated; an array and a scalar give an array |
j - 'k' |
text | jsonb | Object key removed; for an array, matching string elements removed |
j - n |
integer | jsonb | Array element n (0-based) removed |
j #- '{a,b}' |
text array | jsonb | Remove the element at a path |
(source: src/query/engine.rs, jsonb_get_field, jsonb_path_get, jsonb_key_exists, jsonb_any_key_exists, jsonb_all_keys_exist, jsonb_contains, jsonb_concat, jsonb_delete_key, jsonb_delete_path; src/query/json_ops.rs, json_contains_recursive)
The result OID of -> and #> is jsonb whatever the input type; ->> and #>> report text. (source: src/query/engine.rs, infer_expression_type, BinaryOp::JsonbGetField | BinaryOp::JsonbPathGet)
On an hstore value, h -> 'k' returns the value for k as text, and @> tests pair containment. In the MySQL dialect, col->'$.path' and col->>'$.path' take a MySQL JSON path. (source: src/query/engine.rs, jsonb_get_field, jsonb_contains; src/query/mysql_funcs.rs, extract_path, extract_path_text)
Differences from PostgreSQL:
- Negative array indexes (
j -> -1) return NULL; PostgreSQL counts from the end. (source:src/query/engine.rs,jsonb_get_field, index parsed asusize) -
a || bwhere neither side is an array or both are scalars returns NULL; PostgreSQL wraps scalars into an array. (source:src/query/engine.rs,jsonb_concat) -
Not verified:
j #> pathfor a missing path returns a JSONnullvalue rather than SQL NULL in the code read;#>>returns SQL NULL. (source:src/query/engine.rs,jsonb_path_get)
Tested by feature-matrix category functions-json (arrow, arrow2, path, path-text, contains, contained, haskey, extract-int) and diff-corpus queries prefixed type_json_ and type_jsonb_; unit tests in src/query/json_ops.rs (test_json_get and seven others) cover the helper module. (source: test-scripts/feature-matrix/features.py; test-scripts/diff/corpus.py; src/query/json_ops.rs, tests)
JSON functions
| Function | Result | Notes |
|---|---|---|
jsonb_build_object(k1, v1, ...), json_build_object(...)
|
jsonb / json | An odd trailing argument is ignored (PostgreSQL raises) |
jsonb_build_array(...), json_build_array(...)
|
jsonb / json | |
to_jsonb(x), to_json(x)
|
jsonb / json | |
row_to_json(record) |
json | |
jsonb_typeof(j), json_typeof(j)
|
text |
object, array, string, number, boolean, null
|
jsonb_array_length(j), json_array_length(j)
|
int4 | |
jsonb_strip_nulls(j), json_strip_nulls(j)
|
jsonb / json | Removes object fields whose value is null, recursively |
jsonb_set(target, path, new [, create_missing]), json_set(...)
|
jsonb |
path is a text array; new is parsed as JSON when it is text; the fourth argument is ignored and missing keys are always created |
jsonb_object_keys(j), json_object_keys(j)
|
set of text | Set-returning, in a SELECT list |
jsonb_array_elements(j), json_array_elements(j), ..._text
|
set | Set-returning, in FROM or a SELECT list |
jsonb_each(j), json_each(j), ..._text
|
set of (key, value) |
Set-returning, in FROM
|
jsonb_to_record(j), json_to_record(j), ..._recordset
|
set | Needs a column definition list |
json_agg, jsonb_agg, json_object_agg, jsonb_object_agg, json_arrayagg, json_objectagg
|
json / jsonb | Aggregates; see Aggregates |
(source: src/query/engine.rs, evaluate_row_expr arms jsonb_build_object, jsonb_build_array, to_jsonb, row_to_json, jsonb_typeof, jsonb_array_length, jsonb_strip_nulls, jsonb_set, json_set_path; execute_table_function; srf_args, json_srf_kind)
Tested by feature-matrix scenarios functions-json/build-object, build-array, set, array-length, typeof, strip-nulls, array-elements, each, json-agg and jsonb-object-agg. (source: test-scripts/feature-matrix/features.py)
PostgreSQL JSON functions with no arm, which return NULL under the unknown-name rule: jsonb_pretty, jsonb_insert, jsonb_set_lax, json_object, jsonb_object, jsonb_extract_path, json_extract_path, jsonb_extract_path_text, json_extract_path_text, jsonb_path_query, jsonb_path_exists, jsonb_path_match, array_to_json and the other SQL/JSON path functions. The jsonpath operators @? and @@ on jsonb are not implemented; @@ is the text-search operator. (source: src/query/engine.rs, evaluate_row_expr; src/query/sql/lexer.rs)
In the MySQL dialect, mysql_funcs provides MySQL's JSON functions before the main table: json_array, json_array_append, json_array_insert, json_contains, json_contains_path, json_depth, json_extract, json_insert, json_keys, json_length, json_merge, json_merge_patch, json_merge_preserve, json_object, json_overlaps, json_pretty, json_quote, json_remove, json_replace, json_search, json_set, json_type, json_unquote and json_valid. (source: src/query/mysql_funcs.rs)
Arrays
Representation
A SQL array is Value::Array(Vec<Value>) during evaluation and PostgreSQL array text ({1,2,3}, with quoting for elements containing spaces, commas, braces, quotes or the word NULL) in a column cell. Functions that take an array accept either form; pg_array_text_as_array and coerce_to_value_array turn text into elements. (source: src/query/engine.rs, array_value_to_pg_text, pg_array_text_as_array, coerce_to_value_array, parse_pg_array_literal)
The reported element type follows PgTypeOid::array_of: bool, int2, int4, int8, varchar, float4, float8 and numeric arrays have their own OIDs; every other element type is reported as text[]. (source: src/query/types.rs, PgTypeOid::array_of)
Arrays are treated as one-dimensional. A declared dimension (int[3]) is ignored, and array_length(a, d) returns NULL for any d other than 1. Not verified: behaviour of nested (multi-dimensional) array literals. (source: src/query/sql/parser.rs, parse_data_type; src/query/engine.rs, array_length arm)
Constructors and access
| Syntax | Behaviour |
|---|---|
ARRAY[a, b, c] |
Array constructor |
'{1,2,3}'::int[] |
Text array literal |
ARRAY(SELECT ...) |
Expr::ArraySubquery; one element per row of the subquery |
a[i] |
1-based; an index of 0 or less, or past the end, gives NULL |
a[i:j], a[:j], a[i:]
|
Inclusive slice, clamped to the array bounds |
(source: src/plan/logical.rs, Expr::ArraySubscript, Expr::ArraySlice, Expr::ArraySubquery; src/query/engine.rs, array_subscript, array_slice)
Subscripting a JSON array value with [i] is also 1-based, unlike ->, which is 0-based. Subscripting a plain string returns the i-th character, which PostgreSQL does not allow. (source: src/query/engine.rs, array_subscript)
Array operators
| Operator | Behaviour |
|---|---|
a \|\| b |
Concatenation; an element on either side is appended or prepended |
a @> b |
Every element of b is in a
|
a <@ b |
Every element of a is in b
|
a && b |
The arrays share an element |
x = ANY (a), x op ANY (a)
|
True if the comparison holds for some element |
x op ALL (a) |
True if the comparison holds for every element |
@>, <@ and && first check for range values and JSON objects; an operand that parses as a JSON object is treated as jsonb, not as an array. (source: src/query/engine.rs, BinaryOp::Contains, BinaryOp::ContainedBy and BinaryOp::Overlaps arms, array_contains_elems, array_overlaps, Expr::ArrayAny and Expr::ArrayAll arms)
Array functions
| Function | Notes |
|---|---|
array_append(a, x) |
|
array_cat(a, b) |
|
array_remove(a, x) |
Removes every element equal to x
|
array_length(a, 1) |
NULL for an empty array, as in PostgreSQL |
cardinality(a) |
0 for an empty array |
array_to_string(a, sep) |
Delegated to the constant evaluator |
array_position(a, x), array_prepend(x, a), string_to_array(s, sep)
|
Constant expressions only; NULL over a column (see Constant-only functions) |
unnest(a) |
Set-returning, in FROM or a SELECT list; WITH ORDINALITY supported in FROM
|
array_agg(x) |
Aggregate |
array_slice(a, offset, length) |
ClickHouse-style; delegated to the constant evaluator |
array_remove_all, array_add_unique, array_remove_first, array_remove_last
|
Operate on JSON array values (Value::Json), not on SQL arrays; not PostgreSQL functions. Not verified: what calls them; the only references are their own arms |
(source: src/query/engine.rs, evaluate_row_expr arms of the same names, execute_table_function UNNEST)
array_lower, array_upper, array_ndims, array_dims, array_fill, array_replace, array_positions and trim_array have no arm and return NULL. (source: src/query/engine.rs, evaluate_row_expr)
Tested by feature-matrix category functions-array (literal, subscript, length, cardinality, append, prepend, cat, position, remove, to-string, any, all, contains-op, overlap-op, unnest, unnest-with-ordinality, array-agg), DDL-types/array-int, and diff-corpus queries type_array_funcs, type_array_agg and type_string_to_array. The prepend, position and type_string_to_array probes use constant arguments. (source: test-scripts/feature-matrix/features.py; test-scripts/diff/corpus.py)