+ |
+
+
+ json_array_elements ( json )
+ setof json
+
+
+
+ jsonb_array_elements ( jsonb )
+ setof jsonb
+
+ Expands the top-level JSON array into a set of JSON values.
+
+ select * from json_array_elements('[1,true, [2,false]]')
+
-'$.track.segments[*] ? (@.HR > 130)."start time"'
+ value
+-----------
+ 1
+ true
+ [2,false]
-
+
+
- You can use several filter expressions on the same nesting level, if
- required. For example, the following expression selects all segments
- that contain locations with relevant coordinates and high heart rate values:
+ |
+
+
json_array_elements_text
+
+ json_array_elements_text ( json )
+ setof text
+
+
+
jsonb_array_elements_text
+
+ jsonb_array_elements_text ( jsonb )
+ setof text
+
+ Expands the top-level JSON array into a set of text values.
+
+ select * from json_array_elements_text('["foo", "bar"]')
+
-'$.track.segments[*] ? (@.location[1] < 13.4) ? (@.HR > 130)."start time"'
+ value
+-----------
+ foo
+ bar
-
+
+
- Using filter expressions at different nesting levels is also allowed.
- The following example first filters all segments by location, and then
- returns high heart rate values for these segments, if available:
+ |
+
+
+ json_array_length ( json )
+ integer
+
+
+
+ jsonb_array_length ( jsonb )
+ integer
+
+ Returns the number of elements in the top-level JSON array.
+
+ json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]')
+ 5
+
+
+
+ |
+
+
+ json_each ( json )
+ setof key text,
+ value json
+
+
+
+ jsonb_each ( jsonb )
+ setof key text,
+ value jsonb
+
+ Expands the top-level JSON object into a set of key/value pairs.
+
+ select * from json_each('{"a":"foo", "b":"bar"}')
+
-'$.track.segments[*] ? (@.location[1] < 13.4).HR ? (@ > 130)'
+ key | value
+-----+-------
+ a | "foo"
+ b | "bar"
-
+
+
- You can also nest filter expressions within each other:
+ |
+
+
+ json_each_text ( json )
+ setof key text,
+ value text
+
+
+
+ jsonb_each_text ( jsonb )
+ setof key text,
+ value text
+
+ Expands the top-level JSON object into a set of key/value pairs.
+ The returned values will be of
+ type text.
+
+ select * from json_each_text('{"a":"foo", "b":"bar"}')
+
-'$.track ? (exists(@.segments[*] ? (@.HR > 130))).segments.size()'
+ key | value
+-----+-------
+ a | foo
+ b | bar
- This expression returns the size of the track if it contains any
- segments with high heart rate values, or an empty sequence otherwise.
-
+
+
-
PostgreSQL's implementation of SQL/JSON path
- language has the following deviations from the SQL/JSON standard:
-
+ |
+
+
+ json_extract_path ( from_json json, VARIADIC path_elems text[] )
+ json
+
+
+
+ jsonb_extract_path ( from_json jsonb, VARIADIC path_elems text[] )
+ jsonb
+
+ Extracts JSON sub-object at the specified path.
+ (This is functionally equivalent to the #>
+ operator, but writing the path out as a variadic list can be more
+ convenient in some cases.)
+
+ json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}', 'f4', 'f6')
+ "foo"
+
+
-
-
- A path expression can be a Boolean predicate, although the SQL/JSON
- standard allows predicates only in filters. This is necessary for
- implementation of the @@ operator. For example,
- the following jsonpath expression is valid in
+ |
+
+
+ json_extract_path_text ( from_json json, VARIADIC path_elems text[] )
+ text
+
+
+
jsonb_extract_path_text
+
+ jsonb_extract_path_text ( from_json jsonb, VARIADIC path_elems text[] )
+ text
+
+ Extracts JSON sub-object at the specified path as text.
+ (This is functionally equivalent to the #>>
+ operator.)
+
+ json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}', 'f4', 'f6')
+ foo
+
+
+
+ |
+
+
+ json_object_keys ( json )
+ setof text
+
+
+
+ jsonb_object_keys ( jsonb )
+ setof text
+
+ Returns the set of keys in the top-level JSON object.
+
+ select * from json_object_keys('{"f1":"abc","f2":{"f3":"a", "f4":"b"}}')
+
-'$.track.segments[*].HR < 70'
+ json_object_keys
+------------------
+ f1
+ f2
- >
- >
+ >
+ >
-
- There are minor differences in the interpretation of regular
- expression patterns used in like_regex filters, as
- described in .
-
-
-
+ |
+
+
+ json_populate_record ( base anyelement, from_json json )
+ anyelement
+
+
+
+ jsonb_populate_record ( base anyelement, from_json jsonb )
+ anyelement
+
+ Expands the top-level JSON object to a row having the composite type
+ of the base argument. The JSON object
+ is scanned for fields whose names match column names of the output row
+ type, and their values are inserted into those columns of the output.
+ (Fields that do not correspond to any output column name are ignored.)
+ In typical use, the value of base is just
+ NULL, which means that any output columns that do
+ not match any object field will be filled with nulls. However,
+ if base isn't NULL then
+ the values it contains will be used for unmatched columns.
+
+ To convert a JSON value to the SQL type of an output column, the
+ following rules are applied in sequence:
+
+
+ A JSON null value is converted to a SQL null in all cases.
+
+
+
+ If the output column is of type json
+ or jsonb, the JSON value is just reproduced exactly.
+
+
+
+ If the output column is a composite (row) type, and the JSON value
+ is a JSON object, the fields of the object are converted to columns
+ of the output row type by recursive application of these rules.
+
+
+
+ Likewise, if the output column is an array type and the JSON value
+ is a JSON array, the elements of the JSON array are converted to
+ elements of the output array by recursive application of these
+ rules.
+
+
+
+ Otherwise, if the JSON value is a string, the contents of the
+ string are fed to the input conversion function for the column's
+ data type.
+
+
+
+ Otherwise, the ordinary text representation of the JSON value is
+ fed to the input conversion function for the column's data type.
+
+
+
+
+ While the example below uses a constant JSON value, typical use would
+ be to reference a json or jsonb column
+ laterally from another table in the query's FROM
+ clause. Writing json_populate_record in
+ the FROM clause is good practice, since all of the
+ extracted columns are available for use without duplicate function
+ calls.
+
+ select * from json_populate_record(null::myrowtype, '{"a": 1, "b": ["2", "a b"], "c": {"d": 4, "e": "a b c"}}')
+
+ a | b | c
+---+-----------+-------------
+ 1 | {2,"a b"} | (4,"a b c")
+
+
+
-
-
Strict and Lax Modes
- When you query JSON data, the path expression may not match the
- actual JSON data structure. An attempt to access a non-existent
- member of an object or element of an array results in a
- structural error. SQL/JSON path expressions have two modes
- of handling structural errors:
-
+ |
+
+
json_populate_recordset
+
+ json_populate_recordset ( base anyelement, from_json json )
+ setof anyelement
+
+
+
jsonb_populate_recordset
+
+ jsonb_populate_recordset ( base anyelement, from_json jsonb )
+ setof anyelement
+
+ Expands the top-level JSON array of objects to a set of rows having
+ the composite type of the base argument.
+ Each element of the JSON array is processed as described above
+ for json[b]_populate_record.
+
+ select * from json_populate_recordset(null::myrowtype, '[{"a":1,"b":2},{"a":3,"b":4}]')
+
+ a | b
+---+---
+ 1 | 2
+ 3 | 4
+
+
+
+
+ |
+
+
+ json_to_record ( json )
+ record
+
+
+
+ jsonb_to_record ( jsonb )
+ record
+
+ Expands the top-level JSON object to a row having the composite type
+ defined by an AS clause. (As with all functions
+ returning record, the calling query must explicitly
+ define the structure of the record with an AS
+ clause.) The output record is filled from fields of the JSON object,
+ in the same way as described above
+ for json[b]_populate_record. Since there is no
+ input record value, unmatched columns are always filled with nulls.
+
+ select * from json_to_record('{"a":1,"b":[1,2,3],"c":[1,2,3],"e":"bar","r": {"a": 123, "b": "a b c"}}') as x(a int, b text, c int[], d text, r myrowtype)
+
+ a | b | c | d | r
+---+---------+---------+---+---------------
+ 1 | [1,2,3] | {1,2,3} | | (123,"a b c")
+
+
+
+
+ |
+
+
+ json_to_recordset ( json )
+ setof record
+
+
+
+ jsonb_to_recordset ( jsonb )
+ setof record
+
+ Expands the top-level JSON array of objects to a set of rows having
+ the composite type defined by an AS clause. (As
+ with all functions returning record, the calling query
+ must explicitly define the structure of the record with
+ an AS clause.) Each element of the JSON array is
+ processed as described above
+ for json[b]_populate_record.
+
+ select * from json_to_recordset('[{"a":1,"b":"foo"},{"a":"2","c":"bar"}]') as x(a int, b text)
+
+ a | b
+---+-----
+ 1 | foo
+ 2 |
+
+
+
+
+ |
+
+
+ jsonb_set ( target jsonb, path text[], new_value jsonb , create_if_missing boolean )
+ jsonb
+
+ Returns target
+ with the item designated by path
+ replaced by new_value, or with
+ new_value added if
+ create_if_missing is true (which is the
+ default) and the item designated by path
+ does not exist.
+ All earlier steps in the path must exist, or
+ the target is returned unchanged.
+ As with the path oriented operators, negative integers that
+ appear in the path count from the end
+ of JSON arrays.
+ If the last path step is an array index that is out of range,
+ and create_if_missing is true, the new
+ value is added at the beginning of the array if the index is negative,
+ or at the end of the array if it is positive.
+
+ jsonb_set('[{"f1":1,"f2":null},2,null,3]', '{0,f1}','[2,3,4]', false)
+ [{"f1": [2, 3, 4], "f2": null}, 2, null, 3]
+
+ jsonb_set('[{"f1":1,"f2":null},2]', '{0,f3}','[2,3,4]')
+ [{"f1": 1, "f2": null, "f3": [2, 3, 4]}, 2]
+
+
+
+ |
+
+
+ jsonb_set_lax ( target jsonb, path text[], new_value jsonb , create_if_missing boolean , null_value_treatment text )
+ jsonb
+
+ If new_value is not NULL,
+ behaves identically to jsonb_set. Otherwise behaves
+ according to the value
+ of null_value_treatment which must be one
+ of 'raise_exception',
+ 'use_json_null', 'delete_key', or
+ 'return_target'. The default is
+ 'use_json_null'.
+
+ jsonb_set_lax('[{"f1":1,"f2":null},2,null,3]', '{0,f1}',null)
+ [{"f1":null,"f2":null},2,null,3]
+
+ jsonb_set_lax('[{"f1":99,"f2":null},2]', '{0,f3}',null, true, 'return_target')
+ [{"f1": 99, "f2": null}, 2]
+
+
+
+ |
+
+
+ jsonb_insert ( target jsonb, path text[], new_value jsonb , insert_after boolean )
+ jsonb
+
+ Returns target
+ with new_value inserted. If the item
+ designated by the path is an array
+ element, new_value will be inserted before
+ that item if insert_after is false (which
+ is the default), or after it
+ if insert_after is true. If the item
+ designated by the path is an object
+ field, new_value will be inserted only if
+ the object does not already contain that key.
+ All earlier steps in the path must exist, or
+ the target is returned unchanged.
+ As with the path oriented operators, negative integers that
+ appear in the path count from the end
+ of JSON arrays.
+ If the last path step is an array index that is out of range, the new
+ value is added at the beginning of the array if the index is negative,
+ or at the end of the array if it is positive.
+
+ jsonb_insert('{"a": [0,1,2]}', '{a, 1}', '"new_value"')
+ {"a": [0, "new_value", 1, 2]}
+
+ jsonb_insert('{"a": [0,1,2]}', '{a, 1}', '"new_value"', true)
+ {"a": [0, 1, "new_value", 2]}
+
+
+
+ |
+
+
+ json_strip_nulls ( json )
+ json
+
+
+
+ jsonb_strip_nulls ( jsonb )
+ jsonb
+
+ Deletes all object fields that have null values from the given JSON
+ value, recursively. Null values that are not object fields are
+ untouched.
+
+ json_strip_nulls('[{"f1":1,"f2":null},2,null,3]')
+ [{"f1":1},2,null,3]
+
+
+
+ |
+
+
+ jsonb_path_exists ( target jsonb, path jsonpath , vars jsonb , silent boolean )
+ boolean
+
+ Checks whether the JSON path returns any item for the specified JSON
+ value.
+ If the vars argument is specified, it must
+ be a JSON object, and its fields provide named values to be
+ substituted into the jsonpath expression.
+ If the silent argument is specified and
+ is true, the function suppresses the same errors
+ as the @? and @@ operators do.
+
+ jsonb_path_exists('{"a":[1,2,3,4,5]}', '$.a[*] ? (@ >= $min && @ <= $max)', '{"min":2,"max":4}')
+ t
+
+
+
+ |
+
+
+ jsonb_path_match ( target jsonb, path jsonpath , vars jsonb , silent boolean )
+ boolean
+
+ Returns the result of a JSON path predicate check for the specified
+ JSON value. Only the first item of the result is taken into account.
+ If the result is not Boolean, then NULL is returned.
+ The optional vars
+ and silent arguments act the same as
+ for jsonb_path_exists.
+
+ jsonb_path_match('{"a":[1,2,3,4,5]}', 'exists($.a[*] ? (@ >= $min && @ <= $max))', '{"min":2,"max":4}')
+ t
+
+
+
+ |
+
+
+ jsonb_path_query ( target jsonb, path jsonpath , vars jsonb , silent boolean )
+ setof jsonb
+
+ Returns all JSON items returned by the JSON path for the specified
+ JSON value.
+ The optional vars
+ and silent arguments act the same as
+ for jsonb_path_exists.
+
+ select * from jsonb_path_query('{"a":[1,2,3,4,5]}', '$.a[*] ? (@ >= $min && @ <= $max)', '{"min":2,"max":4}')
+
+ jsonb_path_query
+------------------
+ 2
+ 3
+ 4
+
+
+
+
+ |
+
+
+ jsonb_path_query_array ( target jsonb, path jsonpath , vars jsonb , silent boolean )
+ jsonb
+
+ Returns all JSON items returned by the JSON path for the specified
+ JSON value, as a JSON array.
+ The optional vars
+ and silent arguments act the same as
+ for jsonb_path_exists.
+
+ jsonb_path_query_array('{"a":[1,2,3,4,5]}', '$.a[*] ? (@ >= $min && @ <= $max)', '{"min":2,"max":4}')
+ [2, 3, 4]
+
+
+
+ |
+
+
+ jsonb_path_query_first ( target jsonb, path jsonpath , vars jsonb , silent boolean )
+ jsonb
+
+ Returns the first JSON item returned by the JSON path for the
+ specified JSON value. Returns NULL if there are no
+ results.
+ The optional vars
+ and silent arguments act the same as
+ for jsonb_path_exists.
+
+ jsonb_path_query_first('{"a":[1,2,3,4,5]}', '$.a[*] ? (@ >= $min && @ <= $max)', '{"min":2,"max":4}')
+ 2
+
+
+
+ |
+
+
+ jsonb_path_exists_tz ( target jsonb, path jsonpath , vars jsonb , silent boolean )
+ boolean
+
+
+
+ jsonb_path_match_tz ( target jsonb, path jsonpath , vars jsonb , silent boolean )
+ boolean
+
+
+
+ jsonb_path_query_tz ( target jsonb, path jsonpath , vars jsonb , silent boolean )
+ setof jsonb
+
+
+
jsonb_path_query_array_tz
+
+ jsonb_path_query_array_tz ( target jsonb, path jsonpath , vars jsonb , silent boolean )
+ jsonb
+
+
+
jsonb_path_query_first_tz
+
+ jsonb_path_query_first_tz ( target jsonb, path jsonpath , vars jsonb , silent boolean )
+ jsonb
+
+ These functions act like their counterparts described above without
+ the _tz suffix, except that these functions support
+ comparisons of date/time values that require timezone-aware
+ conversions. The example below requires interpretation of the
+ date-only value 2015-08-02 as a timestamp with time
+ zone, so the result depends on the current
+ setting. Due to this dependency, these
+ functions are marked as stable, which means these functions cannot be
+ used in indexes. Their counterparts are immutable, and so can be used
+ in indexes; but they will throw errors if asked to make such
+ comparisons.
+
+ jsonb_path_exists_tz('["2015-08-01 12:00:00 -05"]', '$[*] ? (@.datetime() < "2015-08-02".datetime())')
+ t
+
+
+
+ |
+
+
+ jsonb_pretty ( jsonb )
+ text
+
+ Converts the given JSON value to pretty-printed, indented text.
+
+ jsonb_pretty('[{"f1":1,"f2":null},2]')
+
+[
+ {
+ "f1": 1,
+ "f2": null
+ },
+ 2
+]
+
+
+
+
+ |
+
+
+ json_typeof ( json )
+ text
+
+
+
+ jsonb_typeof ( jsonb )
+ text
+
+ Returns the type of the top-level JSON value as a text string.
+ Possible types are
+ object, array,
+ string, number,
+ boolean, and null.
+ (The null result should not be confused
+ with a SQL NULL; see the examples.)
+
+ json_typeof('-123.4')
+ number
+
+ json_typeof('null'::json)
+ null
+
+ json_typeof(NULL::json) IS NULL
+ t
+
+
+