Operators for Native JSON Values
The following operators are supported onJSON values.
Dot Operator .
Extract the nested JSON value with a given field name from a JSON object. If a nested value with the requested field name does not exist NULL is returned.
Syntax
Parameters
Note that
<field_name> can be double-quoted if necessary, e.g. in order to write <json>."key with spaces" or <json>."1". The dot operator can only be used to extract nested values from JSON objects, i.e. it returns a non-NULL result only if <json> is a JSON object that contains <field_name> as a key. If <json> is a JSON array the operator returns NULL unconditionally, even if <field_name> happens to contains a valid zero-based array index.
Return Type
JSON
- If the
<json>input value isNULLthe operator will returnNULL. - If a field with the requested name does not exist the operator will return
NULL.
Subscript Operator []
Extract a field of a JSON value as another JSON value. Matches against both JSON object entries and JSON array entries in the input. If a field with the requested name or index does not exist NULL is returned.
Syntax
Parameters
This operator does not distinguish between JSON objects and JSON arrays in any way, i.e. it returns a non-NULL result both if
<json> is a JSON object that contains <field_name_or_array_index> as a key and if <json> is a JSON array and <field_name_or_array_index> contains a valid zero-based array index. The type of the <field_name_or_array_index> parameter has no effect on the query result. More specifically, when <json> contains a JSON object the parameter value is always converted to TEXT internally. Conversely, when <json> contains a JSON array the parameter value is always converted to BIGINT internally. If this conversion fails the operator will return NULL. Unlike the dot operator, the subscript operator accepts non-literal <field_name_or_array_index> parameters.
Return Type
JSON
- If the
<json>input value isNULLthe operator will returnNULL. - If a field with the requested name or index does not exist the operator will return
NULL.
Examples
The following table is used in all examples:NULL return value in all cases:
Example
The following query extracts an existing field from a JSON object.
Example
The following queries extract an existing element from a JSON array. Note that the parameter type passed to the subscript operator has no effect on the query result.
Example
The following query extracts an existing field from a JSON object. Note that the parameter type passed to the subscript operator has no effect on the query result.
Example
The following query passes a non-literal
<field_name_or_array_index> parameter to the subscript operator.
Operators for TEXT Values Storing JSON Strings
The following operators are supported onTEXT values storing JSON strings. Note that each invocation of these operators has to re-parse the input JSON string from scratch, making them substantially less efficient than the native JSON operators described above.
Operator ->>
Get value of a JSON field as text.
It never looks into nested JSON documents. Special characters like / in field names are escaped and not interpreted as JSON path separators.
Syntax
Parameters
Return Type
TEXT
- If any input values are
NULL, the function will returnNULL. - If the field name is not found in the JSON document, the function returns
NULL.
Examples
The followingCREATE TABLE and INSERT statements set up the examples.
Example
There is no need to escape
~ and / in the field name when using the ->> operator.
Example
This example returns the nested JSON document associated to the JSON field
value as a TEXT value.
Example
The first
->> operator retrieves the nested JSON document in text form. Applying the second ->> operator on the nested JSON document returns the value associated with the JSON field dyid. The operator returns this value as a string.
Example
The JSON document has no field called
value/dyid. The ->> operator never looks into nested JSON documents.