Temporal JSON Operations

In this section, we give the temporal versions of the functions for the json and jsonb types. We refer to the PostgreSQL documentation for detailed explanations of these functions. We aimed at keeping as much as possible the same syntax for the non-temporal and the temporal versions of these functions as illustrated in this document.

As stated in the section called “Temporalizing Operations”, a common way to generalize the traditional operations to the temporal types is to apply the operation at each instant, which yields a temporal value as result. Consider for example the -> operator that extracts a JSON object field with the given key.

SELECT jsonb '{"unit": "km", "speed": 10}' -> text 'speed';
-- 10

Applying the same operator to a temporal JSON or JSONB value yields the following result

SELECT ttext '{[{"unit": "km", "speed": 10}@2001-01-01, 
  {"unit": "km", "speed": 20}@2001-01-02]}' -> text 'speed';
-- {["10"@2001-01-01, "20"@2001-01-02]}
SELECT tjsonb '{[{"unit": "km", "speed": 10}@2001-01-01, 
  {"unit": "km", "speed": 20}@2001-01-02]}' -> text 'speed';
-- {["10"@2001-01-01, "20"@2001-01-02]}

which is obtained by applying the PostgreSQL operator -> at every instant of the temporal JSON or JSONB value.

When manipulating JSON collections of varying structure, it may be the case that an item is defined in some of the documents but not all. PostgreSQL provides the lax mode for this purpose, where the argument null_value_treatment determines the behavior in the case a function returns a NULL value. The argument can take one of the following values: 'raise_exception', 'use_json_null', 'delete_key', 'return_target', where 'use_json_null' is the default value. In PostgreSQL the lax mode is supported only for update (not query) operations with the function jsonb_set_lax. In MobilityDB, we kept the same semantics for the corresponding function tjsonb_set_lax, but we enabled similar behavior for all temporal JSON operations that may return a null value, except that we replaced the value 'return_target' with 'return_null', since the former is not meaningful for temporal operations. For operators such as ->, the default value 'use_json_null' is used and cannot be changed, whereas for the corresponding functions tjson_object_field and tjsonb_object_field, the last argument specifies the behavior in the case the function returns NULL. We illustrate this behavior for function tjson_object_field below.