Skip to main content

Module json

Module json 

Source
Expand description

v4.14 minimal JSON parser for the -> / ->> operators.

Hand-rolled, no external dep — same policy as the rest of the engine. Supports the JSON grammar from RFC 8259: objects, arrays, strings (with \" / \\ / \/ / \b / \f / \n / \r / \t / \uXXXX escapes), numbers, true / false / null. The parser returns a tree we walk by key (object) or integer index (array); accesses that miss return Value::Null per PG semantics.

path_get(doc, key, as_text) is the public entry. When as_text is true (->> operator), JSON strings unwrap to raw text and other scalars render as their canonical text; when false (->), the result is wrapped back into a Json value (the inner subtree rendered to its canonical JSON string form).

Enums§

JsonValue
MysqlPathStep
v7.37.17 (17.6 siblings) — one step of a MySQL JSON path ($.key, $."quoted key", $[0]).
ParseError

Functions§

array_element_rows
v7.37.17 (17.6 siblings) — set-returning function jsonb_array_elements[_text](json). PG semantics: one row per array element. _text renders scalars as their lexeme and JSON null as SQL NULL; the plain form renders every element (including JSON null) as compact JSON text. Non-array inputs raise an error (PG’s actual behaviour); SQL NULL produces 0 rows.
build_array
json_build_array(...) — variadic; empty → “[]”. Each arg encoded via value_to_json_text.
build_object
canonicalize_jsonb
Canonicalise a jsonb text value the way PostgreSQL does on input: object keys sorted by (length, then bytewise) with duplicate keys collapsed last-wins, , / : whitespace, and numbers normalised to plain decimal (exponents expanded, -00, but trailing zeros from the input scale preserved — 1e2100, 1E-30.001, 1.10 stays 1.10). json keeps its input verbatim; only jsonb runs through this.
canonicalize_value
concat
json_build_object(k, v, k, v, …) — variadic, even-length. NULL key → error (PG: “argument cannot be null”). Values encoded via value_to_json_text. Returns Value::Json. v7.37.17 (17.6 siblings) — jsonb_concat(a, b) — function form of the || operator. Object + object merges keys (right wins on duplicates); array + array appends; array + scalar appends the scalar; scalar + scalar makes a 2-element array (PG semantics).
contains
delete_key
v7.37.17 (17.6 siblings) — jsonb_delete(doc, key) — function form of the - operator. Removes an object key or an array element (by text match for objects, by index for arrays).
delete_path
v7.37.17 (17.6 siblings) — jsonb_delete_path(doc, path[]) — function form of the #- operator. Removes the value at the nested path; missing path leaves the doc unchanged.
each_rows
v7.37.17 (17.6 siblings) — shared body for the four each SRFs. as_text (the *_each_text forms) unwraps scalar values to their lexeme and maps JSON null → SQL NULL; the plain forms render every value (including JSON null) as compact JSON text, which the executor wraps as a jsonb-typed column.
equals
jsonb = jsonb structural equality (PG18-compatible). PG’s jsonb equality is order-INDEPENDENT for object keys but order-SENSITIVE for array elements, and it compares numbers by value (so '1'::jsonb = '1.0'::jsonb is true). json_eq encodes those rules; this parses both operands and delegates. Values reaching here through the ::jsonb cast / a jsonb column are already canonicalised (keys sorted, duplicates collapsed), so object equality is exact.
insert
jsonb_insert(target, path, new_value [, insert_after]) — insert at path. insert_after defaults to false.
jsonb_compare
v7.38 (read01 P6.24) — PG’s jsonb total order (ORDER BY / DISTINCT / btree). First by type rank Null < String < Number < Boolean < Array < Object; then within a type: strings by content, numbers numerically, booleans false < true, arrays by length then element-wise, objects by pair-count then key/value pairwise (keys in canonical stored order). Mirrors the observable behaviour of jsonb.c’s compareJsonbContainers.
jsonb_each_text_rows
v6.4.5 — PG json #> path_text / json #>> path_text. The right-hand side is a PG text-array literal '{a,0,b}' whose elements are walked left-to-right; each element is either an object key or (when it parses as a non-negative integer) an v7.37.43-T4.5 — set-returning function jsonb_each_text(jsonb). PG semantics: for each (key, value) pair in the object, emit one row whose key column is the literal key and value column is the JSON value rendered as text (null → SQL NULL, primitives → their lexeme, nested objects/arrays → JSON text).
jsonpath_canonical
Encode a Value as its canonical JSON text (no surrounding quotes for non-strings). Used by every builder below.
key_exists
v6.4.5 — PG json @> sub_json containment. Returns BOOL. lhs @> rhs is true when every member of rhs is structurally contained in lhs:
keys_all
v7.37.6-A — PG jsonb ?& text[]. Returns BOOL: true iff every one of the listed keys exists at the top level.
keys_any
v7.37.6-A — PG jsonb ?| text[]. Returns BOOL: true iff any one of the listed keys exists at the top level.
mysql_json_array_append
v7.37.17 (17.6 siblings) — MySQL JSON_ARRAY_APPEND(doc, path, val, …). The value at path gains val at the end; a non-array value wraps as [old, val] (MySQL semantics).
mysql_json_array_insert
v7.37.17 (17.6 siblings) — MySQL JSON_ARRAY_INSERT(doc, path, val, …). The path must end in [N]; the value is inserted at position N in the parent array, shifting later elements right (past-the-end appends). A non-array parent is a no-op.
mysql_json_contains
v7.37.17 (17.6 siblings) — MySQL JSON_CONTAINS(target, candidate [, path]).
mysql_json_contains_path
v7.37.17 (17.6 siblings) — MySQL JSON_CONTAINS_PATH(doc, ‘one’|‘all’, path…).
mysql_json_extract
v7.37.17 (17.6 siblings) — MySQL JSON_EXTRACT(doc, path…). One path → the value at that path (or SQL NULL when it misses); several paths → a JSON array of the values that matched (NULL when none did).
mysql_json_insert
mysql_json_merge_patch
v7.37.17 (17.6 siblings) — MySQL JSON_MERGE_PATCH (RFC 7396).
mysql_json_merge_preserve
v7.37.17 (17.6 siblings) — MySQL JSON_MERGE_PRESERVE (and its deprecated JSON_MERGE alias).
mysql_json_overlaps
v7.37.17 (17.6 siblings) — MySQL JSON_OVERLAPS(d1, d2): arrays share any element; objects share any key-value pair; scalars compare equal; an array vs a scalar checks membership.
mysql_json_remove
v7.37.17 (17.6 siblings) — MySQL JSON_REMOVE(doc, path…). Removing the root path $ errors, as in MySQL.
mysql_json_replace
mysql_json_search
v7.37.17 (17.6 siblings) — MySQL JSON_SEARCH(doc, ‘one’|‘all’, pattern [, escape [, path…]]). Returns the path of the first string value LIKE-matching the pattern (‘one’) or a JSON array of all such paths (‘all’); NULL when nothing matches. The optional path args narrow where the walk starts.
mysql_json_set
v7.37.17 (17.6 siblings) — MySQL JSON_SET / JSON_INSERT / JSON_REPLACE (‘$.x’-path forms; the PG jsonb_set text-array spelling stays on crate::json::set).
mysql_json_value
v7.37.17 (17.6 siblings) — MySQL JSON_VALUE(doc, path). Returns the scalar at the path as unquoted text (MySQL’s default RETURNING VARCHAR); containers render as JSON text; a miss is NULL. The RETURNING clause is parser syntax and queued.
mysql_path_get
Walk a parsed JSON document along a MySQL path. Returns None when any step misses.
mysql_path_steps
Parse a MySQL JSON path. Supports $, .key, ."quoted key" and [N]; wildcard steps (*, [*], **) error honestly — they return multiple matches per document and need a different walker shape.
parse
parse_path_vars
v7.39 — parse the vars argument of the jsonb_path_* family into a JsonValue object (NULL → no vars).
path_get
PG json -> key / json ->> key. lhs must be JSON or TEXT containing JSON. rhs is either a TEXT key (object access) or an INT index (array access). as_text=true for ->> (returns Value::Text); false for -> (returns Value::Json).
path_predicate
v7.38 (read01, T8) — evaluate a top-level jsonpath boolean predicate like $.a > 3 (the form the @@ operator / jsonb_path_match takes). Returns Some(bool) when the path is a top-level comparison, or None to let the caller fall back to the ordinary path-query match ($.a ? (...) etc.).
path_predicate_vars
v7.39 — path_predicate with a jsonb vars document.
path_query
v7.17.0 Phase 3.9 — jsonb_path_query(doc, path) — returns the matched JSON values as a TextArray (each element is the JSON encoding of one match).
path_query_array
v7.17.0 Phase 3.9 — jsonb_path_query_array(doc, path) returns the matched values wrapped as a single JSON array.
path_query_array_vars
v7.39 — path_query_array with a jsonb vars document.
path_query_first
v7.17.0 Phase 3.9 — jsonb_path_query_first(doc, path) returns the first matched JSON value as a Json, or NULL on no match.
path_query_first_vars
v7.39 — path_query_first with a jsonb vars document.
path_query_vars
v7.39 — path_query with a jsonb vars document ($name references).
path_walk
array index. Missing or non-existent steps return Value::Null.
set
jsonb_set(target, path, new_value [, create_missing]) — replace at PG text-array path. create_missing defaults to true.
value_to_json_text