RefLib

Reference library for AI

Back

postgresql_16:3.5.14.7 jsonpath Type

PostgreSQL 16 > The SQL Language > Data Types > JSON Types > jsonpath Type

The jsonpath type implements support for the SQL/JSON path language in PostgreSQL to efficiently query JSON data. It provides a binary representation of the parsed SQL/JSON path expression that specifies the items to be retrieved by the path engine from the JSON data for further processing with the SQL/JSON query functions.

The semantics of SQL/JSON path predicates and operators generally follow SQL. At the same time, to provide a natural way of working with JSON data, SQL/JSON path syntax uses some JavaScript conventions:

Numeric literals in SQL/JSON path expressions follow JavaScript rules, which are different from both SQL and JSON in some minor details. For example, SQL/JSON path allows .1 and 1., which are invalid in JSON. Non-decimal integer literals and underscore separators are supported, for example, 1_000_000, 0x1EEE_FFFF, 0o273, 0b100101. In SQL/JSON path (and in JavaScript, but not in SQL proper), there must not be an underscore separator directly after the radix prefix.

An SQL/JSON path expression is typically written in an SQL query as an SQL character string literal, so it must be enclosed in single quotes, and any single quotes desired within the value must be doubled (see (see postgresql_16:3.1.1.2)). Some forms of path expressions require string literals within them. These embedded string literals follow JavaScript/ECMAScript conventions: they must be surrounded by double quotes, and backslash escapes may be used within them to represent otherwise-hard-to-type characters. In particular, the way to write a double quote within an embedded string literal is \", and to write a backslash itself, you must write \\. Other special backslash sequences include those recognized in JavaScript strings: \b, \f, \n, \r, \t, \v for various ASCII control characters, \x*NN* for a character code written with only two hex digits, \u*NNNN* for a Unicode character identified by its 4-hex-digit code point, and \u{*N...*} for a Unicode character code point written with 1 to 6 hex digits.

A path expression consists of a sequence of path elements, which can be any of the following: Path literals of JSON primitive types: Unicode text, numeric, true, false, or null. Path variables listed in (see type-jsonpath-variables). Accessor operators listed in (see type-jsonpath-accessors). jsonpath operators and methods listed in (see postgresql_16:3.6.16.2). Parentheses, which can be used to provide filter expressions or define the order of path evaluation.

For details on using jsonpath expressions with SQL/JSON query functions, see (see postgresql_16:3.6.16.2).

| Variable | Description | | --- | --- | | $ | A variable representing the JSON value being queried (the *context item*). | | $varname | A named variable. Its value can be set by the parameter vars of several JSON processing functions; see (see functions-json-processing-table) for details. | | @ | A variable representing the result of path evaluation in filter expressions. |

| Accessor Operator | Description | | --- | --- | | .*key* ."$*varname*" | Member accessor that returns an object member with the specified key. If the key name matches some named variable starting with $ or does not meet the JavaScript rules for an identifier, it must be enclosed in double quotes to make it a string literal. | | .* | Wildcard member accessor that returns the values of all members located at the top level of the current object. | | . | Recursive wildcard member accessor that processes all levels of the JSON hierarchy of the current object and returns all the member values, regardless of their nesting level. This is a PostgreSQL extension of the SQL/JSON standard. | | .{*level*} .**{*start_level* to *end_level*} | Like .**, but selects only the specified levels of the JSON hierarchy. Nesting levels are specified as integers. Level zero corresponds to the current object. To access the lowest nesting level, you can use the last keyword. This is a PostgreSQL extension of the SQL/JSON standard. | | [*subscript*, ...] | Array element accessor. *subscript* can be given in two forms: *index* or *start_index* to *end_index*. The first form returns a single array element by its index. The second form returns an array slice by the range of indexes, including the elements that correspond to the provided *start_index* and *end_index*. The specified *index* can be an integer, as well as an expression returning a single numeric value, which is automatically cast to integer. Index zero corresponds to the first array element. You can also use the last keyword to denote the last array element, which is useful for handling arrays of unknown length. | | [*] | Wildcard array element accessor that returns all array elements. |

Back