9.15. JSON函式及運算子
Table 9.43shows the operators that are available for use with the two JSON data types (seeSection 8.14).
Table 9.43. json
andjsonb
Operators
Operator | Right Operand Type | Description | Example | Example Result |
|
| Get JSON array element (indexed from zero, negative integers count from the end) |
|
|
|
| Get JSON object field by key |
|
|
|
| Get JSON array element as |
|
|
|
| Get JSON object field as |
|
|
|
| Get JSON object at specified path |
|
|
|
| Get JSON object at specified path as |
|
|
Note
There are parallel variants of these operators for both thejson
andjsonb
types. The field/element/path extraction operators return the same type as their left-hand input (eitherjson
orjsonb
), except for those specified as returningtext
, which coerce the value to text. The field/element/path extraction operators return NULL, rather than failing, if the JSON input does not have the right structure to match the request; for example if no such element exists. The field/element/path extraction operators that accept integer JSON array subscripts all support negative subscripting from the end of arrays.
The standard comparison operators shown inTable 9.1are available forjsonb
, but not forjson
. They follow the ordering rules for B-tree operations outlined atSection 8.14.4.
Some further operators also exist only forjsonb
, as shown inTable 9.44. Many of these operators can be indexed byjsonb
operator classes. For a full description ofjsonb
containment and existence semantics, seeSection 8.14.3.Section 8.14.4describes how these operators can be used to effectively indexjsonb
data.
Table 9.44. Additionaljsonb
Operators
Operator | Right Operand Type | Description | Example | ||||
|
| Does the left JSON value contain the right JSON path/value entries at the top level? |
| ||||
|
| Are the left JSON path/value entries contained at the top level within the right JSON value? |
| ||||
|
| Does the_string_exist as a top-level key within the JSON value? |
| ||||
`? | ` |
| Do any of these array_strings_exist as top-level keys? | `'{"a":1, "b":2, "c":3}'::jsonb ? | array['b', 'c']` | ||
|
| Do all of these array_strings_exist as top-level keys? |
| ||||
` | ` |
| Concatenate two | `'["a", "b"]'::jsonb | '["c", "d"]'::jsonb` | ||
|
| Delete key/value pair or_string_element from left operand. Key/value pairs are matched based on their key value. |
| ||||
|
| Delete multiple key/value pairs or_string_elements from left operand. Key/value pairs are matched based on their key value. |
| ||||
|
| Delete the array element with specified index (Negative integers count from the end). Throws an error if top level container is not an array. |
| ||||
|
| Delete the field or element with specified path (for JSON arrays, negative integers count from the end) |
|
Note
The||
operator concatenates the elements at the top level of each of its operands. It does not operate recursively. For example, if both operands are objects with a common key field name, the value of the field in the result will just be the value from the right hand operand.
Table 9.45shows the functions that are available for creatingjson
andjsonb
values. (There are no equivalent functions forjsonb
, of therow_to_json
andarray_to_json
functions. However, theto_jsonb
function supplies much the same functionality as these functions would.)
Table 9.45. JSON Creation Functions
Function | Description | Example | Example Result |
| Returns the value as |
|
|
| Returns the array as a JSON array. A PostgreSQL multidimensional array becomes a JSON array of arrays. Line feeds will be added between dimension-1 elements if_ |
|
|
| Returns the row as a JSON object. Line feeds will be added between level-1 elements if_ |
|
|
| Builds a possibly-heterogeneously-typed JSON array out of a variadic argument list. |
|
|
| Builds a JSON object out of a variadic argument list. By convention, the argument list consists of alternating keys and values. |
|
|
| Builds a JSON object out of a text array. The array must have either exactly one dimension with an even number of members, in which case they are taken as alternating key/value pairs, or two dimensions such that each inner array has exactly two elements, which are taken as a key/value pair. |
|
|
| This form of |
|
|
Note
array_to_json
androw_to_json
have the same behavior asto_json
except for offering a pretty-printing option. The behavior described forto_json
likewise applies to each individual value converted by the other JSON creation functions.
Note
Thehstoreextension has a cast fromhstore
tojson
, so thathstore
values converted via the JSON creation functions will be represented as JSON objects, not as primitive string values.
Table 9.46shows the functions that are available for processingjson
andjsonb
values.
Table 9.46. JSON Processing Functions
Function | Return Type | Description | Example | Example Result |
|
| Returns the number of elements in the outermost JSON array. |
|
|
|
| Expands the outermost JSON object into a set of key/value pairs. |
| key | value-----+------- a | "foo" b | "bar" |
|
| Expands the outermost JSON object into a set of key/value pairs. The returned values will be of type |
| key | value-----+------- a | foo b | bar |
|
| Returns JSON value pointed to by |
|
|
|
| Returns JSON value pointed to by_ |
|
|
|
| Returns set of keys in the outermost JSON object. |
| json_object_keys------------------ f1 f2 |
|
| Expands the object in |
| a | b | c---+-----------+------------- 1 | {2,"a b"} | (4,"a b c") |
|
| Expands the outermost array of objects in |
| a | b---+--- 1 | 2 3 | 4 |
|
| Expands a JSON array to a set of JSON values. |
| value----------- 1 true [2,false] |
|
| Expands a JSON array to a set of |
| value----------- foo bar |
|
| Returns the type of the outermost JSON value as a text string. Possible types are |
|
|
|
| Builds an arbitrary record from a JSON object (see note below). As with all functions returning |
| a | b | c | d | r---+---------+---------+---+--------------- 1 | [1,2,3] | {1,2,3} | | (123,"a b c") |
|
| Builds an arbitrary set of records from a JSON array of objects (see note below). As with all functions returning |
| a | b---+----- 1 | foo 2 | |
|
| Returns_ |
|
|
|
| Returns |
|
|
|
| Returns |
|
|
|
| Returns_ |
| [ { "f1": 1, "f2": null }, 2, null, 3] |
Note
Many of these functions and operators will convert Unicode escapes in JSON strings to the appropriate single character. This is a non-issue if the input is typejsonb
, because the conversion was already done; but forjson
input, this may result in throwing an error, as noted inSection 8.14.
Note
Injson_populate_record
,json_populate_recordset
,json_to_record
andjson_to_recordset
, type coercion from the JSON is“best effort”and may not result in desired values for some types. JSON keys are matched to identical column names in the target row type. JSON fields that do not appear in the target row type will be omitted from the output, and target columns that do not match any JSON field will simply be NULL.
Note
All the items of thepath
parameter ofjsonb_set
as well asjsonb_insert
except the last item must be present in thetarget
. Ifcreate_missing
is false, all items of thepath
parameter ofjsonb_set
must be present. If these conditions are not met thetarget
is returned unchanged.
If the last path item is an object key, it will be created if it is absent and given the new value. If the last path item is an array index, if it is positive the item to set is found by counting from the left, and if negative by counting from the right --1
designates the rightmost element, and so on. If the item is out of the range -array_length .. array_length -1, and create_missing is true, the new value is added at the beginning of the array if the item is negative, and at the end of the array if it is positive.
Note
Thejson_typeof
function'snull
return value should not be confused with a SQL NULL. While callingjson_typeof('null'::json)
will returnnull
, callingjson_typeof(NULL::json)
will return a SQL NULL.
Note
If the argument tojson_strip_nulls
contains duplicate field names in any object, the result could be semantically somewhat different, depending on the order in which they occur. This is not an issue forjsonb_strip_nulls
sincejsonb
values never have duplicate object field names.
See alsoSection 9.20for the aggregate functionjson_agg
which aggregates record values as JSON, and the aggregate functionjson_object_agg
which aggregates pairs of values into a JSON object, and theirjsonb
equivalents,jsonb_agg
andjsonb_object_agg
.
Last updated