Menu

PostgreSQL jsonb_path_match_tz() Function

Updated on

The PostgreSQL jsonb_path_match_tz() function returns the SQL boolean result of a JSONPath predicate check. Unlike jsonb_path_match(), it supports date/time comparisons that require time zone-aware conversion. Only the first path result is considered; if it is not boolean, the function returns NULL.

jsonb_path_match_tz() Syntax

This is the syntax of the PostgreSQL jsonb_path_match_tz() function:

jsonb_path_match_tz(
     target JSONB
   , path JSONPATH
  [, vars JSONB
  [, silent BOOLEAN]]
) -> BOOLEAN

Parameters

target

Required. The JSONB value to check.

path

Required. The JSON path assertion to perform.

vars

Optional. The variable value used in the path.

silent

Optional. If this parameter is provided and is true, the function suppresses the same errors as the @? and @@ operators.

Return value

The PostgreSQL jsonb_path_match_tz() function returns a boolean value that is the result of a JSON path assertion performed on a specified JSON value. t indicates that the specified JSON value matches the specified JSON path, and f indicates that the specified JSON value does not match the specified JSON path.

If any parameter is NULL, the jsonb_path_match_tz() function will return NULL.

jsonb_path_match_tz() Examples

Basic example

The following example shows how to use the PostgreSQL jsonb_path_match_tz() function to check if a JSON array contains a value greater than 1.

SELECT jsonb_path_match_tz('[1, 2, 3]', 'exists($[*] ? (@ > 1))');
 jsonb_path_match_tz
---------------------
 t

Here, we use path $[*] ? (@ > 2) to get items greater than 2 in the JSON array [1, 2, 3]. Instead, exists($[*] ? (@ > 1)) check if the JSON array contains items greater than 2. The statement is the same as jsonb_path_exists():

SELECT jsonb_path_exists('[1, 2, 3]', '$[*] ? (@ > 1)');
 jsonb_path_match_tz
---------------------
 t

Time zone

These _tz functions are marked STABLE, not IMMUTABLE, because converting a date-only value to timestamptz can depend on the current TimeZone. PostgreSQL therefore does not allow them in index expressions. Their counterparts without _tz are immutable and indexable, but raise an error when a comparison requires time zone-aware conversion. See the PostgreSQL JSON functions documentation.

The following example compares date/time values that need time zone-aware conversion:

select
    jsonb_path_match_tz(
        '["2015-08-01 12:00:00 +00"]',
        'exists($[*] ? (@.datetime() < "2015-08-02".datetime()))'
    );
 jsonb_path_match_tz
---------------------
 t