@@ -18665,10 +18665,15 @@ $.* ? (@ like_regex "^\\d+$")
18665
18665
<literal>JSON_QUERY()</literal>, and <literal>JSON_VALUE()</literal>
18666
18666
described in <xref linkend="functions-sqljson-querying"/> can be used
18667
18667
to query JSON documents. Each of these functions apply a
18668
- <replaceable>path_expression</replaceable> (the query) to a
18669
- <replaceable>context_item</replaceable> (the document); see
18668
+ <replaceable>path_expression</replaceable> (an SQL/JSON path query) to a
18669
+ <replaceable>context_item</replaceable> (the document). See
18670
18670
<xref linkend="functions-sqljson-path"/> for more details on what
18671
- <replaceable>path_expression</replaceable> can contain.
18671
+ the <replaceable>path_expression</replaceable> can contain. The
18672
+ <replaceable>path_expression</replaceable> can also reference variables,
18673
+ whose values are specified with their respective names in the
18674
+ <literal>PASSING</literal> clause that is supported by each function.
18675
+ <replaceable>context_item</replaceable> can be a <type>jsonb</type> value
18676
+ or a character string that can be successfully cast to <type>jsonb</type>.
18672
18677
</para>
18673
18678
18674
18679
<table id="functions-sqljson-querying">
@@ -18691,37 +18696,48 @@ $.* ? (@ like_regex "^\\d+$")
18691
18696
<row>
18692
18697
<entry role="func_table_entry"><para role="func_signature">
18693
18698
<indexterm><primary>json_exists</primary></indexterm>
18694
- <function>json_exists</function> (
18695
- <replaceable>context_item</replaceable>, <replaceable>path_expression</replaceable> <optional> <literal>PASSING</literal> { <replaceable>value</replaceable> <literal>AS</literal> <replaceable>varname</replaceable> } <optional>, ...</optional></optional>
18696
- <optional> { <literal>TRUE</literal> | <literal>FALSE</literal> |<literal> UNKNOWN</literal> | <literal>ERROR</literal> } <literal>ON ERROR</literal> </optional>)
18699
+ <synopsis>
18700
+ <function>JSON_EXISTS</function> (
18701
+ <replaceable>context_item</replaceable>, <replaceable>path_expression</replaceable>
18702
+ <optional> <literal>PASSING</literal> { <replaceable>value</replaceable> <literal>AS</literal> <replaceable>varname</replaceable> } <optional>, ...</optional></optional>
18703
+ <optional>{ <literal>TRUE</literal> | <literal>FALSE</literal> |<literal> UNKNOWN</literal> | <literal>ERROR</literal> } <literal>ON ERROR</literal> </optional>) <returnvalue>boolean</returnvalue>
18704
+ </synopsis>
18697
18705
</para>
18706
+ <itemizedlist>
18707
+ <listitem>
18698
18708
<para>
18699
18709
Returns true if the SQL/JSON <replaceable>path_expression</replaceable>
18700
- applied to the <replaceable>context_item</replaceable> using the
18701
- <literal>PASSING</literal> <replaceable>value</replaceable>s yields any
18702
- items.
18710
+ applied to the <replaceable>context_item</replaceable> yields any
18711
+ items, false otherwise.
18703
18712
</para>
18713
+ </listitem>
18714
+ <listitem>
18704
18715
<para>
18705
18716
The <literal>ON ERROR</literal> clause specifies the behavior if
18706
- an error occurs; the default is to return the <type>boolean</type>
18707
- <literal>FALSE</literal> value. Note that if the
18708
- <replaceable>path_expression</replaceable> is <literal>strict</literal>
18709
- and <literal>ON ERROR</literal> behavior is <literal>ERROR</literal>,
18710
- an error is generated if it yields no items.
18717
+ an error occurs during <replaceable>path_expression</replaceable>
18718
+ evaluation. Specifying <literal>ERROR</literal> will cause an error to
18719
+ be thrown with the appropriate message. Other options include
18720
+ returning <type>boolean</type> values <literal>FALSE</literal> or
18721
+ <literal>TRUE</literal> or the value <literal>UNKNOWN</literal> which
18722
+ is actually an SQL NULL. The default when no <literal>ON ERROR</literal>
18723
+ clause is specified is to return the <type>boolean</type> value
18724
+ <literal>FALSE</literal>.
18711
18725
</para>
18726
+ </listitem>
18727
+ </itemizedlist>
18712
18728
<para>
18713
18729
Examples:
18714
18730
</para>
18715
18731
<para>
18716
- <literal>select json_exists (jsonb '{"key1": [1,2,3]}', 'strict $.key1[*] ? (@ > 2)' )</literal>
18732
+ <literal>JSON_EXISTS (jsonb '{"key1": [1,2,3]}', 'strict $.key1[*] ? (@ > $x)' PASSING 2 AS x )</literal>
18717
18733
<returnvalue>t</returnvalue>
18718
18734
</para>
18719
18735
<para>
18720
- <literal>select json_exists (jsonb '{"a": [1,2,3]}', 'lax $.a[5]' ERROR ON ERROR)</literal>
18736
+ <literal>JSON_EXISTS (jsonb '{"a": [1,2,3]}', 'lax $.a[5]' ERROR ON ERROR)</literal>
18721
18737
<returnvalue>f</returnvalue>
18722
18738
</para>
18723
18739
<para>
18724
- <literal>select json_exists (jsonb '{"a": [1,2,3]}', 'strict $.a[5]' ERROR ON ERROR)</literal>
18740
+ <literal>JSON_EXISTS (jsonb '{"a": [1,2,3]}', 'strict $.a[5]' ERROR ON ERROR)</literal>
18725
18741
<returnvalue></returnvalue>
18726
18742
<programlisting>
18727
18743
ERROR: jsonpath array subscript is out of bounds
@@ -18731,72 +18747,96 @@ ERROR: jsonpath array subscript is out of bounds
18731
18747
<row>
18732
18748
<entry role="func_table_entry"><para role="func_signature">
18733
18749
<indexterm><primary>json_query</primary></indexterm>
18734
- <function>json_query</function> (
18735
- <replaceable>context_item</replaceable>, <replaceable>path_expression</replaceable> <optional> <literal>PASSING</literal> { <replaceable>value</replaceable> <literal>AS</literal> <replaceable>varname</replaceable> } <optional>, ...</optional></optional>
18736
- <optional> <literal>RETURNING</literal> <replaceable>data_type</replaceable> <optional> <literal>FORMAT JSON</literal> <optional> <literal>ENCODING UTF8</literal> </optional> </optional> </optional>
18737
- <optional> { <literal>WITHOUT</literal> | <literal>WITH</literal> { <literal>CONDITIONAL</literal> | <optional><literal>UNCONDITIONAL</literal></optional> } } <optional> <literal>ARRAY</literal> </optional> <literal>WRAPPER</literal> </optional>
18738
- <optional> { <literal>KEEP</literal> | <literal>OMIT</literal> } <literal>QUOTES</literal> <optional> <literal>ON SCALAR STRING</literal> </optional> </optional>
18739
- <optional> { <literal>ERROR</literal> | <literal>NULL</literal> | <literal>EMPTY</literal> { <optional> <literal>ARRAY</literal> </optional> | <literal>OBJECT</literal> } | <literal>DEFAULT</literal> <replaceable>expression</replaceable> } <literal>ON EMPTY</literal> </optional>
18740
- <optional> { <literal>ERROR</literal> | <literal>NULL</literal> | <literal>EMPTY</literal> { <optional> <literal>ARRAY</literal> </optional> | <literal>OBJECT</literal> } | <literal>DEFAULT</literal> <replaceable>expression</replaceable> } <literal>ON ERROR</literal> </optional>)
18750
+ <synopsis>
18751
+ <function>JSON_QUERY</function> (
18752
+ <replaceable>context_item</replaceable>, <replaceable>path_expression</replaceable>
18753
+ <optional> <literal>PASSING</literal> { <replaceable>value</replaceable> <literal>AS</literal> <replaceable>varname</replaceable> } <optional>, ...</optional></optional>
18754
+ <optional> <literal>RETURNING</literal> <replaceable>data_type</replaceable> <optional> <literal>FORMAT JSON</literal> <optional> <literal>ENCODING UTF8</literal> </optional> </optional> </optional>
18755
+ <optional> { <literal>WITHOUT</literal> | <literal>WITH</literal> { <literal>CONDITIONAL</literal> | <optional><literal>UNCONDITIONAL</literal></optional> } } <optional> <literal>ARRAY</literal> </optional> <literal>WRAPPER</literal> </optional>
18756
+ <optional> { <literal>KEEP</literal> | <literal>OMIT</literal> } <literal>QUOTES</literal> <optional> <literal>ON SCALAR STRING</literal> </optional> </optional>
18757
+ <optional> { <literal>ERROR</literal> | <literal>NULL</literal> | <literal>EMPTY</literal> { <optional> <literal>ARRAY</literal> </optional> | <literal>OBJECT</literal> } | <literal>DEFAULT</literal> <replaceable>expression</replaceable> } <literal>ON EMPTY</literal> </optional>
18758
+ <optional> { <literal>ERROR</literal> | <literal>NULL</literal> | <literal>EMPTY</literal> { <optional> <literal>ARRAY</literal> </optional> | <literal>OBJECT</literal> } | <literal>DEFAULT</literal> <replaceable>expression</replaceable> } <literal>ON ERROR</literal> </optional>) <returnvalue>jsonb</returnvalue>
18759
+ </synopsis>
18741
18760
</para>
18761
+ <itemizedlist>
18762
+ <listitem>
18742
18763
<para>
18743
18764
Returns the result of applying the SQL/JSON
18744
18765
<replaceable>path_expression</replaceable> to the
18745
- <replaceable>context_item</replaceable> using the
18746
- <literal>PASSING</literal> <replaceable>value</replaceable>s.
18766
+ <replaceable>context_item</replaceable>.
18767
+ </para>
18768
+ </listitem>
18769
+ <listitem>
18770
+ <para>
18771
+ By default, the result is returned as a value of type <type>jsonb</type>,
18772
+ though the <literal>RETURNING</literal> clause can be used to return
18773
+ as some other type to which it can be successfully coerced.
18747
18774
</para>
18775
+ </listitem>
18776
+ <listitem>
18748
18777
<para>
18749
- If the path expression returns multiple SQL/JSON items, it might be
18750
- necessary to wrap the result using the <literal>WITH WRAPPER</literal>
18751
- clause to make it a valid JSON string. If the wrapper is
18752
- <literal>UNCONDITIONAL</literal>, an array wrapper will always be
18753
- applied, even if the returned value is already a single JSON object
18754
- or an array. If it is <literal>CONDITIONAL</literal>, it will not be
18755
- applied to a single JSON object or an array.
18756
- <literal>UNCONDITIONAL</literal> is the default.
18778
+ If the path expression may return multiple values, it might be necessary
18779
+ to wrap those values using the <literal>WITH WRAPPER</literal> clause to
18780
+ make it a valid JSON string, because the default behavior is to not wrap
18781
+ them, as if <literal>WITHOUT WRAPPER</literal> were specified. The
18782
+ <literal>WITH WRAPPER</literal> clause is by default taken to mean
18783
+ <literal>WITH UNCONDITIONAL WRAPPER</literal>, which means that even a
18784
+ single result value will be wrapped. To apply the wrapper only when
18785
+ multiple values are present, specify <literal>WITH CONDITIONAL WRAPPER</literal>.
18786
+ Getting multiple values in result will be treated as an error if
18787
+ <literal>WITHOUT WRAPPER</literal> is specified.
18757
18788
</para>
18789
+ </listitem>
18790
+ <listitem>
18758
18791
<para>
18759
18792
If the result is a scalar string, by default, the returned value will
18760
18793
be surrounded by quotes, making it a valid JSON value. It can be made
18761
18794
explicit by specifying <literal>KEEP QUOTES</literal>. Conversely,
18762
18795
quotes can be omitted by specifying <literal>OMIT QUOTES</literal>.
18763
- Note that <literal>OMIT QUOTES</literal> cannot be specified when
18764
- <literal>WITH WRAPPER</literal> is also specified.
18796
+ To ensure that the result is a valid JSON value, <literal>OMIT QUOTES</literal>
18797
+ cannot be specified when <literal>WITH WRAPPER</literal> is also
18798
+ specified.
18765
18799
</para>
18800
+ </listitem>
18801
+ <listitem>
18766
18802
<para>
18767
- The <literal>RETURNING</literal> clause can be used to specify the
18768
- <replaceable>data_type</replaceable> of the result value. By default,
18769
- the returned value will be of type <type>jsonb</type>.
18803
+ The <literal>ON EMPTY</literal> clause specifies the behavior if
18804
+ evaluating <replaceable>path_expression</replaceable> yields an empty
18805
+ set. The <literal>ON ERROR</literal> clause specifies the behavior
18806
+ if an error occurs when evaluating <replaceable>path_expression</replaceable>,
18807
+ when coercing the result value to the <literal>RETURNING</literal> type,
18808
+ or when evaluating the <literal>ON EMPTY</literal> expression if the
18809
+ <replaceable>path_expression</replaceable> evaluation returns an empty
18810
+ set.
18770
18811
</para>
18812
+ </listitem>
18813
+ <listitem>
18771
18814
<para>
18772
- The <literal>ON EMPTY</literal> clause specifies the behavior if
18773
- evaluating <replaceable>path_expression</replaceable> yields no value
18774
- at all. The default when <literal>ON EMPTY</literal> is not specified
18775
- is to return a null value.
18776
- </para>
18777
- <para>
18778
- The <literal>ON ERROR</literal> clause specifies the
18779
- behavior if an error occurs when evaluating
18780
- <replaceable>path_expression</replaceable>, including the operation to
18781
- coerce the result value to the output type, or during the execution of
18782
- <literal>ON EMPTY</literal> behavior (that is caused by empty result
18783
- of <replaceable>path_expression</replaceable> evaluation). The default
18784
- when <literal>ON ERROR</literal> is not specified is to return a null
18785
- value.
18815
+ For both <literal>ON EMPTY</literal> and <literal>ON ERROR</literal>,
18816
+ specifying <literal>ERROR</literal> will cause an error to be thrown with
18817
+ the appropriate message. Other options include returning an SQL NULL, an
18818
+ empty array (<literal>EMPTY <optional>ARRAY</optional></literal>),
18819
+ an empty object (<literal>EMPTY OBJECT</literal>), or a user-specified
18820
+ expression (<literal>DEFAULT</literal> <replaceable>expression</replaceable>)
18821
+ that can be coerced to jsonb or the type specified in <literal>RETURNING</literal>.
18822
+ The default when <literal>ON EMPTY</literal> or <literal>ON ERROR</literal>
18823
+ is not specified is to return an SQL NULL value.
18786
18824
</para>
18825
+ </listitem>
18826
+ </itemizedlist>
18787
18827
<para>
18788
18828
Examples:
18789
18829
</para>
18790
18830
<para>
18791
- <literal>select json_query (jsonb '[1,[2,3],null]', 'lax $[*][1]' WITH CONDITIONAL WRAPPER)</literal>
18831
+ <literal>JSON_QUERY (jsonb '[1,[2,3],null]', 'lax $[*][$off]' PASSING 1 AS off WITH CONDITIONAL WRAPPER)</literal>
18792
18832
<returnvalue>[3]</returnvalue>
18793
18833
</para>
18794
18834
<para>
18795
- <literal>select json_query (jsonb '{"a": "[1, 2]"}', 'lax $.a' OMIT QUOTES); </literal>
18835
+ <literal>JSON_QUERY (jsonb '{"a": "[1, 2]"}', 'lax $.a' OMIT QUOTES)</literal>
18796
18836
<returnvalue>[1, 2]</returnvalue>
18797
18837
</para>
18798
18838
<para>
18799
- <literal>select json_query (jsonb '{"a": "[1, 2]"}', 'lax $.a' RETURNING int[] OMIT QUOTES ERROR ON ERROR); </literal>
18839
+ <literal>JSON_QUERY (jsonb '{"a": "[1, 2]"}', 'lax $.a' RETURNING int[] OMIT QUOTES ERROR ON ERROR)</literal>
18800
18840
<returnvalue></returnvalue>
18801
18841
<programlisting>
18802
18842
ERROR: malformed array literal: "[1, 2]"
@@ -18808,55 +18848,76 @@ DETAIL: Missing "]" after array dimensions.
18808
18848
<row>
18809
18849
<entry role="func_table_entry"><para role="func_signature">
18810
18850
<indexterm><primary>json_value</primary></indexterm>
18811
- <function>json_value</function> (
18812
- <replaceable>context_item</replaceable>, <replaceable>path_expression</replaceable>
18813
- <optional> <literal>PASSING</literal> { <replaceable>value</replaceable> <literal>AS</literal> <replaceable>varname</replaceable> } <optional>, ...</optional></optional>
18814
- <optional> <literal>RETURNING</literal> <replaceable>data_type</replaceable> </optional>
18815
- <optional> { <literal>ERROR</literal> | <literal>NULL</literal> | <literal>DEFAULT</literal> <replaceable>expression</replaceable> } <literal>ON EMPTY</literal> </optional>
18816
- <optional> { <literal>ERROR</literal> | <literal>NULL</literal> | <literal>DEFAULT</literal> <replaceable>expression</replaceable> } <literal>ON ERROR</literal> </optional>)
18851
+ <synopsis>
18852
+ <function>JSON_VALUE</function> (
18853
+ <replaceable>context_item</replaceable>, <replaceable>path_expression</replaceable>
18854
+ <optional> <literal>PASSING</literal> { <replaceable>value</replaceable> <literal>AS</literal> <replaceable>varname</replaceable> } <optional>, ...</optional></optional>
18855
+ <optional> <literal>RETURNING</literal> <replaceable>data_type</replaceable> </optional>
18856
+ <optional> { <literal>ERROR</literal> | <literal>NULL</literal> | <literal>DEFAULT</literal> <replaceable>expression</replaceable> } <literal>ON EMPTY</literal> </optional>
18857
+ <optional> { <literal>ERROR</literal> | <literal>NULL</literal> | <literal>DEFAULT</literal> <replaceable>expression</replaceable> } <literal>ON ERROR</literal> </optional>) <returnvalue>text</returnvalue>
18858
+ </synopsis>
18817
18859
</para>
18860
+ <itemizedlist>
18861
+ <listitem>
18818
18862
<para>
18819
18863
Returns the result of applying the SQL/JSON
18820
18864
<replaceable>path_expression</replaceable> to the
18821
- <replaceable>context_item</replaceable> using the
18822
- <literal>PASSING</literal> <replaceable>value</replaceable>s.
18865
+ <replaceable>context_item</replaceable>.
18823
18866
</para>
18867
+ </listitem>
18868
+ <listitem>
18824
18869
<para>
18825
- The extracted value must be a single <acronym>SQL/JSON</acronym>
18826
- scalar item; an error is thrown if that's not the case. If you expect
18827
- that extracted value might be an object or an array, use the
18828
- <function>json_query</function> function instead.
18870
+ Only use <function>JSON_VALUE()</function> if the extracted value is
18871
+ expected to be a single <acronym>SQL/JSON</acronym> scalar item;
18872
+ getting multiple values will be treated as an error. If you expect that
18873
+ extracted value might be an object or an array, use the
18874
+ <function>JSON_QUERY</function> function instead.
18829
18875
</para>
18876
+ </listitem>
18877
+ <listitem>
18830
18878
<para>
18831
- The <literal>RETURNING</literal> clause can be used to specify the
18832
- <replaceable>data_type</replaceable> of the result value. By default,
18833
- the returned value will be of type <type>text</type>.
18879
+ By default, the result, which must be a single scalar value, is
18880
+ returned as a value of type <type>text</type>, though the
18881
+ <literal>RETURNING</literal> clause can be used to return as some
18882
+ other type to which it can be successfully coerced.
18834
18883
</para>
18884
+ </listitem>
18885
+ <listitem>
18835
18886
<para>
18836
18887
The <literal>ON ERROR</literal> and <literal>ON EMPTY</literal>
18837
18888
clauses have similar semantics as mentioned in the description of
18838
- <function>json_query</function>.
18889
+ <function>JSON_QUERY</function>, except the set of values returned in
18890
+ lieu of throwing an error is different.
18839
18891
</para>
18892
+ </listitem>
18893
+ <listitem>
18840
18894
<para>
18841
- Note that scalar strings returned by <function>json_value </function>
18895
+ Note that scalar strings returned by <function>JSON_VALUE </function>
18842
18896
always have their quotes removed, equivalent to specifying
18843
- <literal>OMIT QUOTES</literal> in <function>json_query </function>.
18897
+ <literal>OMIT QUOTES</literal> in <function>JSON_QUERY </function>.
18844
18898
</para>
18899
+ </listitem>
18900
+ </itemizedlist>
18845
18901
<para>
18846
18902
Examples:
18847
18903
</para>
18848
18904
<para>
18849
- <literal>select json_value (jsonb '"123.45"', '$' RETURNING float)</literal>
18905
+ <literal>JSON_VALUE (jsonb '"123.45"', '$' RETURNING float)</literal>
18850
18906
<returnvalue>123.45</returnvalue>
18851
18907
</para>
18852
18908
<para>
18853
- <literal>select json_value (jsonb '"03:04 2015-02-01"', '$.datetime("HH24:MI YYYY-MM-DD")' RETURNING date)</literal>
18909
+ <literal>JSON_VALUE (jsonb '"03:04 2015-02-01"', '$.datetime("HH24:MI YYYY-MM-DD")' RETURNING date)</literal>
18854
18910
<returnvalue>2015-02-01</returnvalue>
18855
18911
</para>
18856
18912
<para>
18857
- <literal>select json_value(jsonb '[1,2]', 'strict $[*]' DEFAULT 9 ON ERROR)</literal>
18913
+ <literal>JSON_VALUE(jsonb '[1,2]', 'strict $[$off]' PASSING 1 as off)</literal>
18914
+ <returnvalue>2</returnvalue>
18915
+ </para>
18916
+ <para>
18917
+ <literal>JSON_VALUE(jsonb '[1,2]', 'strict $[*]' DEFAULT 9 ON ERROR)</literal>
18858
18918
<returnvalue>9</returnvalue>
18859
- </para></entry>
18919
+ </para>
18920
+ </entry>
18860
18921
</row>
18861
18922
</tbody>
18862
18923
</tgroup>
@@ -18871,6 +18932,14 @@ DETAIL: Missing "]" after array dimensions.
18871
18932
clause.
18872
18933
</para>
18873
18934
</note>
18935
+ <note>
18936
+ <para>
18937
+ <function>JSON_VALUE()</function> returns an SQL NULL if
18938
+ <replaceable>path_expression</replaceable> returns a JSON
18939
+ <literal>null</literal>, whereas <function>JSON_QUERY()</function> returns
18940
+ the JSON <literal>null</literal> as is.
18941
+ </para>
18942
+ </note>
18874
18943
</sect2>
18875
18944
18876
18945
<sect2 id="functions-sqljson-table">
0 commit comments