jq function
View as Markdown
Unreleased
This feature will be released in
v26.52.
It may not be available in your region yet.
The release is scheduled to complete by November 25, 2026.
jq applies a jq program to a
jsonb value and returns the result as jsonb. Use it to
write per-row logic, such as sorting, filtering, or deduplicating a list, that
would otherwise require subqueries. Using jq can be more memory efficient
than expressing the same logic in SQL. For a walkthrough, see Write functions
with jq.
Syntax
jq ( <input>, <program> )
| Syntax element | Description |
|---|---|
<input>
|
A jsonb value to pass to the program.
|
<program>
|
A text literal containing a
jq program. The program is compiled when
the query is planned, so it cannot come from a column or a query
parameter.
|
Return value
jq returns jsonb.
- If
<input>isNULL,jqreturnsNULL. - If the program produces no value,
jqreturnsNULL. - If the program produces more than one value,
jqreturns an error. To return several values, collect them into an array ([...]) or an object.
Details
- Program literal. The program must be a string literal. Dollar-quoted
strings (
$$ ... $$) are useful for multi-line programs. Concatenating literals, for example'.' || 'a', is allowed. - One input, one output. The program sees only
<input>. To use several columns, pack them withjsonb_build_objectorjsonb_build_array. To return several values, return an object and extract its fields with->or->>. - Evaluation.
jqis evaluated one row at a time, inside the dataflow that computes the query. It does not maintain state of its own. - Implementation. Programs run on jaq, a Rust implementation of jq. jaq differs from jq in some details.
- Unavailable filters. Filters whose result depends on the environment or
on the current time are not available:
env,now,localtime,strflocaltime,strptime,input, andinputs.debugandstderrhave no effect. Module imports (import,include) are not supported. - Numbers. Integers keep full precision. Other numbers keep their precision until the program does arithmetic on them, which converts them to 64-bit floating point.
- Errors. Syntax errors and undefined filters are reported when the query
is planned. Runtime errors, such as
error("..."),halt_error, or output that is not valid JSON (for example,nan), are reported as query errors.
WARNING! jq programs have no evaluation limit. A program that does not terminate keeps
the cluster replica’s worker busy indefinitely, and a program that recurses
without bound can crash the replica. Test programs with ad hoc
SELECT
queries before using them in materialized views or indexes.
Examples
Sort a list of objects by a field and keep one field:
SELECT jq(
'{"items": [{"name": "pear", "price": 3}, {"name": "fig", "price": 1}, {"name": "kiwi", "price": 2}]}',
'.items | sort_by(.price) | map(.name)'
) AS names;
names
-----------------------
["fig","kiwi","pear"]
Filter a list. Wrap the program in [...] to collect several outputs into one
array:
SELECT jq('[1, 2, 3]', '[.[] | select(. > 1)]') AS result;
result
--------
[2,3]
Combine several columns into the input, and return several values as an object:
SELECT jq(
jsonb_build_object('name', 'ada', 'langs', jsonb_build_array('en', 'fr')),
'{name: .name | ascii_upcase, langs: (.langs | length)}'
) AS result;
result
--------------------------
{"langs":2,"name":"ADA"}