All Products
Search
Document Center

PolarDB:JSON functions and expressions

Last Updated:Oct 24, 2025

PolarDB provides Oracle-compatible JSON functions and expressions to process semi-structured data at the database layer. You can use these functions and expressions to validate data formats, dynamically construct JSON objects, or aggregate data from multiple rows into a JSON array.

Scope

The minor engine version of your PolarDB for PostgreSQL (Compatible with Oracle) cluster must be 2.0.14.18.37.0 or later.

Note

You can check the minor engine version in the console or using the SHOW polardb_version; statement. If the minor engine version of your cluster does not meet the requirement, upgrade the minor engine version.

Validate JSON data

You can use the IS JSON expression to determine whether data is in a valid JSON format.

Syntax

expr IS [ NOT ] JSON

Examples

  • Validate a valid JSON string

    -- Determine whether the string is valid JSON.
    SELECT '{"name": "Alice", "age": 30}' IS JSON;

    The expected result is:

     polar_is_json 
    ---------------
     t
  • Validate a JSON string

    -- Use IS NOT JSON to identify invalid JSON.
    SELECT '{name}' IS NOT JSON;

    The expected result is:

     polar_is_not_json 
    -------------------
     t
  • Handle NULL values

    IS JSON and IS NOT JSON also return NULL when the input is NULL.

    -- Handle NULL values.
    SELECT NULL IS JSON;
    SELECT NULL IS NOT JSON;

Build JSON objects

You can use the JSON_OBJECT expression to combine key-value pairs or table column data into a JSON object.

Prerequisites

You must enable the polar_enable_ora_json_funcs parameter. Navigate to the Settings Management > Parameters page in the PolarDB console to set cluster parameters.

Syntax

JSON_OBJECT (
    [ { [ KEY ] key_expr VALUE val_expr | key_expr [ ':' val_expr ] } [ FORMAT JSON ] , ... ]
    [ { NULL | ABSENT } ON NULL ] -- JSON_on_null_clause
    [ RETURNING { JSON | VARCHAR2 | CLOB | BLOB } ] -- JSON_returning_clause
    [ STRICT ]
    [ WITH UNIQUE KEYS ]
)

Parameters

Parameter/Clause

Description

KEY key_expr VALUE val_expr

Defines a key-value pair.

  • key_expr: The name of the key. It must be a string constant.

  • val_expr: The value of the property. This can be an expression that evaluates to a number, string, date, or timestamp. If val_expr is omitted, key_expr is treated as a table column, where the column name is used as the KEY and the column's value is used as the VALUE.

  • The key_expr and val_expr are separated by the VALUE keyword or a colon. Together, they form a key-value pair. If there are multiple key-value pairs, they are separated by commas.

FORMAT JSON

This optional clause can be specified for each key-value pair to indicate that val_expr itself is also JSON and should not be escaped again when the final object is generated.

JSON_on_null_clause

An optional clause that specifies the behavior when val_expr is NULL:

  • NULL ON NULL: Retains the key in the final JSON object and sets its value to null.

  • ABSENT ON NULL (default): Omits the key-value pair from the final JSON object.

JSON_returning_clause

An optional clause that specifies the return value type. The available types are:

Note

The current incompatibility between PolarDB and Oracle is that PolarDB provides only syntax compatibility, without changing the return value type of an expression, and always returns the JSON type. However, PolarDB supports implicit conversion from the JSON type to the VARCHAR2, CLOB, and BLOB types. Therefore, the returned JSON type can usually be implicitly converted to the target type, which typically does not affect your services.

  • RETURNING JSON

  • RETURNING VARCHAR2

  • RETURNING VARCHAR2(<type_mode>)

  • RETURNING CLOB

  • RETURNING BLOB

STRICT

An optional clause. It performs a strict check to ensure that the expression result conforms to the JSON format. If the check fails, an error is reported. By default, no check is performed.

WITH UNIQUE KEYS

An optional clause. It checks for duplicate keys. If a duplicate key exists, an error is reported. By default, no check is performed.

Examples

  • Basic usage: Build a simple object

    -- Combine several key-value pairs into a JSON object.
    SELECT JSON_OBJECT(KEY 'name' VALUE 'Tim', 'age' : 20);

    The expected result is:

        polar_json_object    
    -------------------------
     {"name":"Tim","age":20}
  • Advanced usage: Handle NULL values and embedded JSON

    -- Handle NULL values and embed an existing JSON string.
    SELECT JSON_OBJECT(
      'user_id' : 101,
      'profile' : '{"city": "Shanghai"}' FORMAT JSON,
      'manager_id' : NULL,          
      'department_id' : NULL
      ABSENT ON NULL
      STRICT
      WITH UNIQUE KEYS) Result
    FROM dual;

    The expected result is:

                         result                     
    ------------------------------------------------
     {"user_id":101,"profile":{"city": "Shanghai"}}

Serialize JSON data

You can use JSON_SERIALIZE to transform JSON data into a string for easier reading or transmission.

Prerequisites

You must enable the polar_enable_ora_json_funcs parameter. You can navigate to the Settings Management > Parameters page in the PolarDB console to set cluster parameters.

Syntax

JSON_SERIALIZE (
    expr
    [ RETURNING { JSON | VARCHAR2 | CLOB | BLOB } ] -- JSON_returning_clause
    [ PRETTY ]
)

Parameters

Parameter/Clause

Description

expr

The JSON data to be converted. The data must be a string literal or of the JSON, VARCHAR2, CLOB, or BLOB type.

JSON_returning_clause

This optional clause specifies the type of the return value. If you do not specify it, the default is VARCHAR2(4000). If you specify VARCHAR2 but do not specify type_mode, the default is also VARCHAR2(4000).

  • RETURNING JSON

  • RETURNING VARCHAR2

  • RETURNING VARCHAR2(<type_mode>)

  • RETURNING CLOB

  • RETURNING BLOB

PRETTY

An optional clause. It formats the output JSON string by adding line breaks and indents. By default, the string is not formatted.

Note

PolarDB currently has an incompatibility with Oracle: If JSON data contains duplicate keys and the STRICT clause is specified, the duplicate keys are lost.

Examples

-- Format a compact JSON string into a readable form.
SELECT JSON_SERIALIZE('{"a":[1,2,3,4], "b":{"c": "d"}}' RETURNING VARCHAR2 PRETTY);

The expected result is:

 jsonb_pretty 
--------------
 {
   "a": [
     1,
     2,
     3,
     4
   ],
   "b": {
     "c": "d"
   }
 }

Aggregate into a JSON array

You can use the JSON_ARRAYAGG aggregate function to aggregate a column from a multi-row query result into a JSON array.

Prerequisites

You must enable the polar_enable_ora_json_funcs parameter. You can navigate to the Settings Management > Parameters page in the PolarDB console to set cluster parameters.

Syntax

JSON_ARRAYAGG ( expr )

Examples

Assume that you have an employee table and want to aggregate the ages of all employees into a JSON array.

-- Aggregate the age column from multiple rows into a JSON array.
SELECT JSON_ARRAYAGG(age)
FROM (
  SELECT '12' AS age FROM DUAL UNION ALL
  SELECT '-12.3' FROM DUAL UNION ALL
  SELECT '13.5' FROM DUAL UNION ALL
  SELECT '15.7' FROM DUAL
);

The expected result is:

     polar_json_arrayagg      
------------------------------
 ["12","-12.3","13.5","15.7"]

Use JSON types in PL/SQL

In a PL/SQL environment, you can use the TREAT AS expression to cast a JSON object to a specified JSON type. Currently, type conversion is supported only between the JSON_ELEMENT_T base class and its derived classes, such as JSON_OBJECT_T and JSON_ARRAY_T.

Scenarios

This expression lets you operate on JSON data in an object-oriented way within a PL/SQL code block and switch between different JSON type views.

Examples

The following example shows how to convert between JSON_OBJECT_T and JSON_ELEMENT_T.

-- This example shows how to convert a specific JSON object type to a generic element type and then convert it back in PL/SQL.
DECLARE
  l_json_element JSON_ELEMENT_T;
  l_json_object  JSON_OBJECT_T;
  l_json_object2 JSON_OBJECT_T;
BEGIN
  -- 1. Create and fill a JSON_OBJECT_T object.
  l_json_object := JSON_OBJECT_T();
  l_json_object.put('name', 'Kevin');
  l_json_object.put('number', 35);

  -- 2. Use TREAT AS to convert it to the generic JSON_ELEMENT_T type.
  l_json_element := TREAT(l_json_object AS JSON_ELEMENT_T);
  DBMS_OUTPUT.PUT_LINE('JSON_ELEMENT_T from JSON_OBJECT_T: ' || l_json_element.to_string);

  -- 3. Convert the generic element type back to the specific JSON_OBJECT_T type.
  l_json_object2 := TREAT(l_json_element AS JSON_OBJECT_T);
  DBMS_OUTPUT.PUT_LINE('JSON_OBJECT_T from JSON_ELEMENT_T: ' || l_json_object2.to_string);
END;
/