15 APEX_JSON

This package includes utilities that parse and generate JSON.

Package Overview and Examples

To read from a string that contains JSON data, first use parse() to convert the string to an internal format. Then use the get_% routines (e.g. get_varchar2(), get_number(), ...) to access the data and find_paths_like() to search.

Alternatively, use to_xmltype() to convert a JSON string to an xmltype.

This package also contains procedures to generate JSON-formatted output. Use the overloaded open_%(), close_%() and write() procedures for writing.

Example 1

This example parses a JSON string and prints the value of member variable "a".

DECLARE
    s varchar2(32767) := '{ "a": 1, "b": ["hello", "world"]}';
BEGIN
    apex_json.parse(s);
    sys.dbms_output.put_line('a is '||apex_json.get_varchar2(p_path => 'a'));
END;

Example 2

This example converts a JSON string to XML and uses XMLTABLE to query member values.

select col1, col2
from xmltable (
        '/json/row'
        passing apex_json.to_xmltype('[{"col1": 1, "col2": "hello"},'||
                '{"col1": 2, "col2": "world"}]')
        columns
           col1 number path '/row/col1',
           col2 varchar2(5) path '/row/col2' );

Example 3

This example writes a nested JSON object to the HTP buffer.

BEGIN
    apex_json.open_object;        -- {
    apex_json.write('a', 1);    --   "a":1
    apex_json.open_array('b');  --  ,"b":[
    apex_json.open_object;    --    {
    apex_json.write('c',2); --      "c":2
    apex_json.close_object;   --    }
    apex_json.write('hello'); --   ,"hello"
    apex_json.write('world'); --   ,"world"
    apex_json.close_all;          --  ]
                          -- }
END;

Constants and Data Types

Parser Interface

The following are constants used for the parser interface:

subtype t_kind is binary_integer range 1 .. 7;
c_null     constant t_kind := 1;
c_true     constant t_kind := 2;
c_false    constant t_kind := 3;
c_number   constant t_kind := 4;
c_varchar2 constant t_kind := 5;
c_object   constant t_kind := 6;
c_array    constant t_kind := 7;

Storage for JSON Data

JSON data is stored in an index by varchar2 table. The JSON values are stored as records. The discriminator "kind" determines whether the value is null, true, false, a number, a varchar2, an object or an array. It depends on "kind" which record fields are used and how. If not explicitly mentioned below, the other record fields' values are undefined:

* c_null: -

* c_true: -

* c_false: -

* c_number: number_value contains the number value

* c_varchar2: varchar2_value contains the varchar2 value

* c_object: object_members contains the names of the object's members

* c_array: number_value contains the array length

type t_value is record (
   kind           t_kind,
   number_value   number,
   varchar2_value varchar2(32767),
   object_members wwv_flow_t_varchar2 );
type t_values is table of t_value index by varchar2(32767);

Default Format for Dates

c_date_iso8601 constant varchar2(30) := 'yyyy-mm-dd"T"hh24:mi:ss"Z"';

Default JSON Values Table

g_values t_values;

Errors Thrown for PARSE()

e_parse_error     exception;
pragma exception_init(e_parse_error, -20987);

CLOSE_ALL Procedure

This procedure closes all objects and arrays up to the outermost nesting level.

Syntax

APEX_JSON.CLOSE_ALL;

Parameters

None.

Example

See "Package Overview and Examples".

CLOSE_ARRAY Procedure

This procedure writes a close bracket symbol as follows:

]

Syntax

APEX_JSON.CLOSE_ARRAY ();

Parameters

None.

Example

See "Package Overview and Examples".

CLOSE_OBJECT Procedure

This procedure writes a close curly bracket symbol as follows:

}

Syntax

APEX_JSON.CLOSE_OBJECT ();

Parameters

None.

Example

See "Package Overview and Examples".

DOES_EXIST Function

This function determines whether the given path points to an existing value.

Syntax

APEX_JSON.DOES_EXIST (
   p_path             IN VARCHAR2,
   p0                 IN VARCHAR2 DEFAULT NULL,
   p1                 IN VARCHAR2 DEFAULT NULL,
   p2                 IN VARCHAR2 DEFAULT NULL,
   p3                 IN VARCHAR2 DEFAULT NULL,
   p4                 IN VARCHAR2 DEFAULT NULL,
   p_values           IN t_values DEFAULT g_values ) 
RETURN BOOLEAN;

Parameters

Table 15-1 DOES_EXIST Function Parameters

Parameter Description

p_path

Index into p_values.

p[0-4]

Each %N in p_path is replaced by pN and every i-th %s or %d is replaced by the p[i-1].

p_values

Parsed JSON members. The default is g_values.


Returns

Table 15-2 DOES_EXIST Function Returns

Return Description

TRUE

Given path points to an existing value.

FALSE

Given path does not point to an existing value


Example

This example parses a JSON string and prints whether it contains values under a path.

DECLARE 
    j apex_json.t_values; 
BEGIN 
    apex_json.parse(j, '{ "items": [ 1, 2, { "foo": true } ] }'); 
    if apex_json.does_exist(p_path => 'items[%d].foo', p0 => 3, p_values => 
j) then 
        dbms_output.put_line('found items[3].foo'); 
    end if; 
END;

FIND_PATHS_LIKE Function

This function returns paths into p_values that match a given pattern.

Syntax

APEX_JSON.FIND_PATHS_LIKE (
    p_return_path      IN VARCHAR2,
    p_subpath          IN VARCHAR2 DEFAULT NULL,
    p_value            IN VARCHAR2 DEFAULT NULL,
    p_values           IN t_values DEFAULT g_values )
RETURN wwv_flow_t_varchar2;

Parameters

Table 15-3 FIND_PATHS_LIKE Function Parameters

Parameter Description

p_return_path

Search pattern for the return path..

p_subpath

Search pattern under p_return_path (optional).

p_value

Search pattern for value (optional).

p_values

Parsed JSON members. The default is g_values.


Returns/Raised Errors

Table 15-4 FIND_PATHS_LIKE Function Returns and Raised Errors

Return Description

apex_t_varchar2

Table of paths that match the pattern.

VALUE_ERROR

Raises this error if p_values(p_path) is not an array or object.


Example

This example parses a JSON string, finds paths that match a pattern, and prints the values under the paths.

DECLARE
    j            apex_json.t_values;
    l_paths apex_t_varchar2;
BEGIN
    apex_json.parse(j, '{ "items": [ { "name": "Amulet of Yendor", "magical": true }, '||
                                     { "name": "Slippers",  "magical": "rather not" } ]}');
    l_paths := apex_json.find_paths_like (
        p_values         => j,
        p_return_path => 'items[%]',
        p_subpath       => '.magical',
        p_value           => 'true' );
    dbms_output.put_line('Magical items:');
    for i in 1 .. l_paths.count loop
        dbms_output.put_line(apex_json.get_varchar2(p_values => j, p_path => l_paths(i)||'.name')); 
    end loop;
END;

FREE_OUTPUT Procedure

Frees output resources. Call this procedure after process if you are using INITIALIZE_CLOB_OUTPUT to write to a temporary CLOB.

Syntax

free_output;

Example

This example configures APEX_JSON for CLOB output, generate JSON, print the CLOB with DBMS_OUTPUT, and finally free the CLOB.

BEGIN
  apex_json.initialize_clob_output;

  apex_json.open_object;
  apex_json.write('hello', 'world');
  apex_json.close_object;

  dbms_output.put_line(apex_json.get_clob_output);

 apex_json.free_output;
 END;

FLUSH Procedure

This procedure flushes pending changes. Note that close procedures automatically flush.

Syntax

APEX_JSON.FLUSH

Parameters

None.

Example

This example writes incomplete JSON.

BEGIN
  apex_json.open_object;
  apex_json.write('attr', 'value');
  apex_json.flush;
  sys.htp.p('the "}" is missing');
END;

GET_BOOLEAN Function

This function returns a boolean number value.

Syntax

APEX_JSON.GET_BOOLEAN (
    p_path             IN VARCHAR2,
    p0                 IN VARCHAR2 DEFAULT NULL,
    p1                 IN VARCHAR2 DEFAULT NULL,
    p2                 IN VARCHAR2 DEFAULT NULL,
    p3                 IN VARCHAR2 DEFAULT NULL,
    p4                 IN VARCHAR2 DEFAULT NULL,
    p_default          IN BOOLEAN  DEFAULT NULL,
    p_values           IN t_values DEFAULT g_values )
RETURN BOOLEAN;

Parameters

Table 15-5 GET_BOOLEAN Function Parameters

Parameter Description

p_path

Index into p_values.

p[0-4]

Each %N in p_path is replaced by pN and every i-th %s or %d is replaced by the p[i-1].

p_default

The default value if the member does not exist.

p_values

Parsed JSON members. The default is g_values.


Returns

Table 15-6 GET_BOOLEAN Function Returns

Return Description

TRUE

.Value at the given path position.

FALSE

Value at the given path position.

NULL

Value at the given path position.

VALUE_ERROR

Raises this error if p_values(p_path) is not boolean.


Example

This example parses a JSON string and prints the boolean value at a position.

DECLARE 
    j apex_json.t_values; 
BEGIN 
    apex_json.parse(j, '{ "items": [ 1, 2, { "foo": true } ] }'); 
    if apex_json.get_boolean(p_path=>'items[%d].foo', p0=>3,p_values=>j) then 
        dbms_output.put_line('items[3].foo is true'); 
END IF; 
END; 

GET_CLOB_OUTPUT Function

Returns the temporary CLOB that you created with INITIALIZE_CLOB_OUTPUT.

Syntax

function get_clob_output
    return clob;

Example

This example configures APEX_JSON for CLOB output, generate JSON, print the CLOB with DBMS_OUTPUT, and finally free the CLOB.

BEGIN
  apex_json.initialize_clob_output;

  apex_json.open_object;
  apex_json.write('hello', 'world');
  apex_json.close_object;

  dbms_output.put_line(apex_json.get_clob_output);

 apex_json.free_output;
 END;

GET_COUNT Function

This function returns the number of array elements or object members.

Syntax

APEX_JSON.GET_COUNT (
    p_path             IN VARCHAR2,
    p0                 IN VARCHAR2 DEFAULT NULL,
    p1                 IN VARCHAR2 DEFAULT NULL,
    p2                 IN VARCHAR2 DEFAULT NULL,
    p3                 IN VARCHAR2 DEFAULT NULL,
    p4                 IN VARCHAR2 DEFAULT NULL,
    p_values           IN t_values DEFAULT g_values )
RETURN NUMBER;

Parameters

Table 15-7 GET_COUNT Function Parameters

Parameter Description

p_path

Index into p_values.

p[0-4]

Each %N in p_path is replaced by pN and every i-th %s or %d is replaced by the p[i-1].

p_values

Parsed JSON members. The default is g_values.


Returns/Raised Errors

Table 15-8 GET_COUNT Function Returns and Raised Errors

Return Description

NUMBER

The number of array elements or object members or null if the array or object could not be found

VALUE_ERROR

Raises this error if p_values(p_path) is not an array or object.


Example

This example parses a JSON string and prints the number of members at positions.

DECLARE 
    j apex_json.t_values; 
BEGIN 
    apex_json.parse(j, '{ "foo": 3, "bar": [1, 2, 3, 4] }'); 
    dbms_output.put_line(apex_json.get_count(p_path=>'.',p_values=>j)); -- 2 (foo and bar) 
    dbms_output.put_line(apex_json.get_count(p_path=>'bar',p_values=>j)); -- 4 
END; 

GET_DATE Function

This function returns a date member value.

Syntax

APEX_JSON.GET_DATE (
    p_path             IN VARCHAR2,
    p0                 IN VARCHAR2 DEFAULT NULL,
    p1                 IN VARCHAR2 DEFAULT NULL,
    p2                 IN VARCHAR2 DEFAULT NULL,
    p3                 IN VARCHAR2 DEFAULT NULL,
    p4                 IN VARCHAR2 DEFAULT NULL,
    p_default          IN DATE     DEFAULT NULL,
    p_format           IN VARCHAR2 DEFAULT c_date_iso8601,
    p_values           IN t_values DEFAULT g_values )
RETURN DATE;

Parameters

Table 15-9 GET_DATE Function Parameters

Parameter Description

p_path

Index into p_values.

p[0-4]

Each %N in p_path is replaced by pN and every i-th %s or %d is replaced by the p[i-1].

p_default

The default value if the member does not exist.

p_format

The date format mask.

p_values

Parsed JSON members. The default is g_values.


Returns/Raised Errors

Table 15-10 GET_DATE Function Returns and Raised Errors

Return Description

DATE

.Returns the date.

VALUE_ERROR

Raises this error if p_values(p_path) is not a date.


Example

This example parses a JSON string and prints the value at a position.

DECLARE 
    j apex_json.t_values; 
BEGIN 
    apex_json.parse(j, '{ "items": [ 1, 2, { "foo": "2014-04-29T10:08:00Z" }] }'); 
    
dbms_output.put_line(to_char(apex_json.get_date(p_path=>'items[%d].foo',p0=>3, p_values=>j), 'DD-Mon-YYYY')); 
END;

GET_MEMBERS Function

This function returns the table of OBJECT_MEMBERS names for an object.

Syntax

APEX_JSON.GET_MEMBERS (
    p_path             IN VARCHAR2,
    p0                 IN VARCHAR2 DEFAULT NULL,
    p1                 IN VARCHAR2 DEFAULT NULL,
    p2                 IN VARCHAR2 DEFAULT NULL,
    p3                 IN VARCHAR2 DEFAULT NULL,
    p4                 IN VARCHAR2 DEFAULT NULL,
    p_values           IN t_values DEFAULT g_values )
RETURN WWV_FLOW_T_VARCHAR2;

Parameters

Table 15-11 GET_MEMBERS Function Parameters

Parameter Description

p_path

Index into p_values.

p[0-4]

Each %N in p_path is replaced by pN and every i-th %s or %d is replaced by the p[i-1].

p_values

Parsed JSON members. The default is g_values.


Returns/Raised Errors

Table 15-12 GET_MEMBERS Function Returns and Raised Errors

Return Description

OBJECT_MEMBERS

The OBJECT_MEMBERS of the object or null if the object could not be found.

VALUE_ERROR

Raises this error if p_values(p_path) is not an array or object.


Example

This example parses a JSON string and prints members at positions.

DECLARE 
    j apex_json.t_values; 
BEGIN 
    apex_json.parse(j, '{ "foo": 3, "bar": [1, 2, 3, 4] }'); 
    dbms_output.put_line(apex_json.get_members(p_path=>'.',p_values=>j)(1)); -- foo
    dbms_output.put_line(apex_json.get_members(p_path=>'.',p_values=>j)(2)); -- bar 
END; 

GET_NUMBER Function

This function returns a numeric number value.

Syntax

APEX_JSON.GET_NUMBER (
    p_path             IN VARCHAR2,
    p0                 IN VARCHAR2 DEFAULT NULL,
    p1                 IN VARCHAR2 DEFAULT NULL,
    p2                 IN VARCHAR2 DEFAULT NULL,
    p3                 IN VARCHAR2 DEFAULT NULL,
    p4                 IN VARCHAR2 DEFAULT NULL,
    p_default          IN BOOLEAN DEFAULT NULL,
    p_values           IN t_values DEFAULT g_values )
RETURN NUMBER;

Parameters

Table 15-13 GET_NUMBER Function Parameters

Parameter Description

p_path

Index into p_values.

p[0-4]

Each %N in p_path is replaced by pN and every i-th %s or %d is replaced by the p[i-1].

p_default

The default value if the member does not exist.

p_values

Parsed JSON members. The default is g_values.


Returns/Raised Errors

Table 15-14 GET_NUMBER Function Returns and Raised Errors

Return Description

NUMBER

The value at the given path position.

VALUE_ERROR

Raises this error if p_values(p_path) is not a number.


Example

This example parses a JSON string and prints the value at a position.

DECLARE 
    j apex_json.t_values; 
BEGIN 
    apex_json.parse(j, '{ "items": [ 1, 2, { "foo": 42 } ] }'); 
    dbms_output.put_line(apex_json.get_number(p_path=>'items[%d].foo',p0=> 3,p_values=>j)); 
END; 

GET_VALUE Function

This function returns the t_value.

Syntax

APEX_JSON.GET_VALUE (
    p_path             IN VARCHAR2,
    p0                 IN VARCHAR2 DEFAULT NULL,
    p1                 IN VARCHAR2 DEFAULT NULL,
    p2                 IN VARCHAR2 DEFAULT NULL,
    p3                 IN VARCHAR2 DEFAULT NULL,
    p4                 IN VARCHAR2 DEFAULT NULL,
    p_values           IN t_values DEFAULT g_values )
RETURN t_value;

Parameters

Table 15-15 GET_VALUE Function Parameters

Parameter Description

p_path

Index into p_values.

p[0-4]

Each %N in p_path is replaced by pN and every i-th %s or %d is replaced by the p[i-1].

p_values

Parsed JSON members. The default is g_values.


Returns/Raised Errors

Table 15-16 GET_VALUE Function Returns and Raised Errors

Return Description

t_value

The t_value at the given path position. The record attributes are null if no data is found.

VALUE_ERROR

Raises this error if p_values(p_path) is not an array or object.


Example

This example parses a JSON string and prints attributes of values at positions.

DECLARE
    j apex_json.t_values; 
    v apex_json.t_value; 
BEGIN 
    apex_json.parse(j, '{ "foo": 3, "bar": [1, 2, 3, 4] }'); 
    v := apex_json.get_value(p_path=>'bar[%d]',p0=> 2,p_values=>j); -- returns the t_value for bar[2] 
    dbms_output.put_line(v.number_value); -- 2 
    v := apex_json.get_value(p_path=>'does.not.exist',p_values=>j); 
    dbms_output.put_line(case when v.kind is null then 'not found!' end); 
END; 

GET_VARCHAR2 Function

This function returns a varchar2 member value. This function converts boolean and number values to varchar2 values.

Syntax

APEX_JSON.GET_VARCHAR2 (
    p_path             IN VARCHAR2,
    p0                 IN VARCHAR2 DEFAULT NULL,
    p1                 IN VARCHAR2 DEFAULT NULL,
    p2                 IN VARCHAR2 DEFAULT NULL,
    p3                 IN VARCHAR2 DEFAULT NULL,
    p4                 IN VARCHAR2 DEFAULT NULL,
    p_default          IN BOOLEAN  DEFAULT NULL,
    p_values           IN t_values DEFAULT g_values )
RETURN VARCHAR2;

Parameters

Table 15-17 GET_VARCHAR2 Function Parameters

Parameter Description

p_path

Index into p_values.

p[0-4]

Each %N in p_path is replaced by pN and every i-th %s or %d is replaced by the p[i-1].

p_default

The default value if the member does not exist.

p_values

Parsed JSON members. The default is g_values.


Returns/Raised Errors

Table 15-18 GET_VARCHAR2 Function Returns and Raised Errors

Return Description

VARCHAR2

This is the value at the given path position.

VALUE_ERROR

Raises this error if p_values(p_path) is not an array or object.


Example

This example parses a JSON string and prints the value at a position.

DECLARE 
    j apex_json.t_values; 
BEGIN 
    apex_json.parse(j, '{ "items": [ 1, 2, { "foo": 42 } ] }'); 
    dbms_output.put_line(apex_json.get_varchar2(p_path=>'items[%d].foo',p0=> 3,p_values=>j)); 
END;

INITIALIZE_CLOB_OUTPUT Procedure

Initialize the output interface to write to a temporary CLOB. the default is to write to SYS.HTP. If using CLOB output, you should call FREE_OUTPUT() at the end to free the CLOB.

Syntax

APEX_JSON.INITIALIZE_CLOB_OUTPUT (
    p_dur         in pls_integer default sys.dbms_lob.call,
    p_cache       in boolean     default true,
    p_indent      in pls_integer default null );

Parameters

Table 15-19 INITIALIZE_CLOB_OUTPUT Procedure Parameters

Parameter Description

p_dur

Duration of the temporary CLOB. this can be DBMS_LOB.SESSION or DBMS_LOB.CALL (the default).

p_cache

Specifies if the lob should be read into buffer cache or not.

p_indent

Indent level. Defaults to 2 if debug is turned on, 0 otherwise.


Example

This example configures APEX_JSON for CLOB output, generate JSON, print the CLOB with DBMS_OUTPUT, and finally free the CLOB.

BEGIN
  apex_json.initialize_clob_output;

  apex_json.open_object;
  apex_json.write('hello', 'world');
  apex_json.close_object;

  dbms_output.put_line(apex_json.get_clob_output);

 apex_json.free_output;
 END;

INITIALIZE_OUTPUT Procedure

This procedure initializes the output interface. You only have to call this procedure if you want to modify the parameters below. Initially, output is already configured with the defaults mentioned in the parameter table.

Syntax

APEX_JSON.INITIALIZE_OUTPUT (
    p_http_header     in boolean     default true,
    p_http_cache      in boolean     default false,
    p_http_cache_etag in varchar2    default null, 
    p_indent          in pls_integer default null );

Parameters

Table 15-20 INITIALIZE_OUTPUT Procedure Parameters

Parameter Description

p_http_header

If TRUE (the default), write an application/JSON mime type header.

p_http_cache

This parameter is only relevant if p_write_header is TRUE. If TRUE, writes Cache-Control: max-age=315360000. If FALSE (the default), writes Cache-Control: no-cache. Otherwise, does not write Cache-Control.

http_cache_etag

If not null, writes an etag header. This parameter is only used if P_HTTP_CACHE is true.

p_indent

Indent level. Defaults to 2, if debug is turned on, otherwise defaults to 0.


Example

This example configures APEX_JSON to not emit default headers, because they are written directly.

BEGIN
  apex_json.initialize_output (
      p_http_header => false );
 
  sys.owa_util.mime_header('application/json', false);
  sys.owa_util.status_line(429, 'Too Many Requests');
  sys.owa_util.http_header_close;
  --
  apex_json.open_object;
  apex_json.write('maxRequestsPerSecond', 10);
  apex_json.close_object;
END;

OPEN_ARRAY Procedure

This procedure writes an open bracket symbol as follows:

[

Syntax

APEX_JSON.OPEN_ARRAY (
    p_name     IN VARCHAR2 DEFAULT NULL );

Parameters

Table 15-21 OPEN_ARRAY Procedure Parameters

Parameter Description

p_name

If not null, write an object attribute name and colon before the opening bracket.


Example

This example performs a write { "array":[ 1 ,[ ] ] }.

BEGIN
  apex_json.open_object; -- {
  apex_json.open_array('array'); -- "array": [
  apex_json.write(1); -- 1
  apex_json.open_array; -- , [
  apex_json.close_array; -- ]
  apex_json.close_array; -- ]
  apex_json.close_object; -- }
END;

OPEN_OBJECT Procedure

This procedure writes an open curly bracket symbol as follows:

{

Syntax

APEX_JSON.OPEN_OBJECT (
    p_name     IN VARCHAR2 DEFAULT NULL );

Parameters

Table 15-22 OPEN_OBJECT Procedure Parameters

Parameter Description

p_name

If not null, write an object attribute name and colon before the opening brace.


Example

This example performs a write { "obj": { "obj-attr": "value" }}.

BEGIN
  apex_json.open_object; -- {
  apex_json.open_object('obj'); -- "obj": {
  apex_json.write('obj-attr', 'value'); -- "obj-attr": "value"
  apex_json.close_all; -- }}
END;

PARSE Procedure Signature 1

This procedure parses a JSON-formatted varchar2 or clob and puts the members into p_values.

Syntax

APEX_JSON.PARSE (
    p_values   in out nocopy t_values,
    p_source   in varchar2,
    p_strict   in boolean default true );

APEX_JSON.PARSE (
    p_values   in out nocopy t_values,
    p_source   in clob,
    p_strict   in boolean default true );

Parameters

Table 15-23 PARSE Procedure Parameters

Parameter Description

p_values

An index by varchar2 result array which contains the JSON members and values. The default is g_values.

p_source

The JSON source (varchar2 or clob)

p_strict

If TRUE (default), enforce strict JSON rules


Example

This example parses JSON and prints member values.

DECLARE
    l_values apex_json.t_values;
BEGIN
    apex_json.parse (
        p_values => l_values,
        p_source => '{ "type": "circle", "coord": [10, 20] }' );
    sys.htp.p('Point at '||
        apex_json.get_number (
            p_values => l_values,
            p_path   => 'coord[1]')||
        ','||
        apex_json.get_number (
            p_values => l_values,
            p_path   => 'coord[2]'));
END;

PARSE Procedure Signature 2

This procedure parses a JSON-formatted varchar2 or clob and puts the members into the package global g_values. This simplified API works similar to the parse() procedure for signature 1, but saves the developer from declaring a local variable for parsed JSON data and passing it to each JSON API call.

Syntax

APEX_JSON.PARSE (
    p_source   IN VARCHAR2,
    p_strict   IN BOOLEAN DEFAULT TRUE );

APEX_JSON.PARSE (
    p_source   IN CLOB,
    p_strict   IN BOOLEAN DEFAULT TRUE );

Parameters

Table 15-24 PARSE Procedure Parameters

Parameter Description

p_source

The JSON source (varchar2 or clob).

p_strict

If TRUE (default), enforce strict JSON rules.


Example

This example parses JSON and prints member values.

apex_json.parse('{ "type": "circle", "coord": [10, 20] }');
sys.htp.p('Point at '||
    apex_json.get_number(p_path=>'coord[1]')||
    ','||
    apex_json.get_number(p_path=>'coord[2]'));

STRINGIFY Function Signature 1

This function converts a string to an escaped JSON value.

Syntax

APEX_JSON.STRINGIFY (
    p_value  IN VARCHAR2 )
RETURN VARCHAR2;

Parameters

Table 15-25 STRINGIFY Function Parameters

Parameter Description

p_value

The string to be converted.


Returns

Table 15-26 STRINGIFY Function Returns

Return Description

VARCHAR2

The converted and escaped JSON value.


Example

This example is a query that returns a JSON varchar2 value.

select apex_json.stringify('line 1'||chr(10)||'line 2') from dual;

STRINGIFY Function Signature 2

This function converts a number to an escaped JSON value.

Syntax

APEX_JSON.STRINGIFY (
    p_value  IN NUMBER )
RETURN VARCHAR2;

Parameters

Table 15-27 STRINGIFY Function Parameters

Parameter Description

p_value

The number to be converted.


Returns

Table 15-28 STRINGIFY Function Returns

Return Description

VARCHAR2

The converted and escaped JSON value.


Example

This example is a query that returns a JSON number value.

select apex_json.stringify(-1/10) from dual

STRINGIFY Function Signature 3

This function converts a date to an escaped JSON value.

Syntax

APEX_JSON.STRINGIFY (
    p_value  IN DATE,
    p_format IN VARCHAR2 DEFAULT c_date_iso8601 )
RETURN VARCHAR2;

Parameters

Table 15-29 STRINGIFY Function Parameters

Parameter Description

p_value

The date value to be converted.


Returns

Table 15-30 STRINGIFY Function Returns

Return Description

VARCHAR2

The converted and escaped JSON value.


Example

This example is a query that returns a JSON varchar2 value that is suitable to be converted to dates.

select apex_json.stringify(sysdate) from dual

STRINGIFY Function Signature 4

This function converts a boolean value to an escaped JSON value.

Syntax

APEX_JSON.STRINGIFY (
    p_value  IN BOOLEAN,
RETURN VARCHAR2;

Parameters

Table 15-31 STRINGIFY Function Parameters

Parameter Description

p_value

The boolean value to be converted.


Returns

Table 15-32 STRINGIFY Function Returns

Return Description

VARCHAR2

The converted and escaped JSON value.


Example

This example demonstrates printing JSON boolean values.

BEGIN
  sys.htp.p(apex_json.stringify(true));
  sys.htp.p(apex_json.stringify(false));
END;

TO_XMLTYPE Function

This procedure parses a JSON-formatted varchar2 or CLOB and converts it to an xmltype.

Syntax

APEX_JSON.TO_XMLTYPE (
    p_source   IN VARCHAR2,
    p_strict   IN BOOLEAN DEFAULT TRUE )
RETURN sys.xmltype;

APEX_JSON.TO_XMLTYPE (
    p_source   IN CLOB,
    p_strict   IN BOOLEAN DEFAULT TRUE )
RETURN sys.xmltype;

Parameters

Table 15-33 TO_XMLTYPE Function Parameters

Parameter Description

p_source

The JSON source (VARCHAR2 or CLOB)

p_strict

If TRUE (default), enforce strict JSON rules


Returns

Table 15-34 TO_XMLTYPE Function Returns

Return Description

sys.xmltype

An xmltype representation of the JSON data.


Example

This example parses JSON and prints the XML representation.

DECLARE
    l_xml xmltype;
BEGIN
    l_xml := apex_json.to_xmltype('{ "items": [ 1, 2, { "foo": true } ] }');
    dbms_output.put_line(l_xml.getstringval);
END;

WRITE Procedure Signature 1

This procedure writes an array attribute of type VARCHAR2.

Syntax

APEX_JSON.WRITE (
    p_value    IN VARCHAR2 );

Parameters

Table 15-35 WRITE Procedure Parameters

Parameter Description

p_value

The value to be written.


Example

This example writes an array containing 1, "two", "long text", false, the current date and a JSON representation of an xml document.

DECLARE
  l_clob clob := 'long text';
  l_xml sys.xmltype := sys.xmltype('<obj><foo>1</foo><bar>2</bar></obj>');
BEGIN
  apex_json.open_array; -- [
  apex_json.write(1); -- 1
  apex_json.write('two'); -- , "two"
  apex_json.write(l_clob); -- , "long text"
  apex_json.write(false); -- , false
  apex_json.write(sysdate); -- , "2014-05-05T05:36:08Z"
  apex_json.write(l_xml); -- , { "foo": 1, "bar": 2 }
  apex_json.close_array; -- ]
END;

WRITE Procedure Signature 2

This procedure writes an array attribute. of type clob.

Syntax

APEX_JSON.WRITE (
    p_value    IN CLOB );

Parameters

Table 15-36 WRITE Procedure Parameters

Parameter Description

p_value

The value to be written.


Example

See "WRITE Procedure Signature 1".

WRITE Procedure Signature 3

This procedure writes an array attribute of type NUMBER.

Syntax

APEX_JSON.WRITE (
    p_value    IN NUMBER );

Parameters

Table 15-37 WRITE Procedure Parameters

Parameter Description

p_value

The value to be written.


Example

See "WRITE Procedure Signature 1".

WRITE Procedure Signature 4

This procedure writes an array attribute. of type date

Syntax

APEX_JSON.WRITE (
    p_value    IN DATE,
    p_format   IN VARCHAR2 DEFAULT c_date_iso8601 );

Parameters

Table 15-38 WRITE Procedure Parameters

Parameter Description

p_value

The value to be written.

p_format

The date format mask (default c_date_iso8601).


Example

See "WRITE Procedure Signature 1".

WRITE Procedure Signature 5

This procedure writes an array attribute of type boolean.

Syntax

APEX_JSON.WRITE (
    p_value    IN BOOLEAN );

Parameters

Table 15-39 WRITE Procedure Parameters

Parameter Description

p_value

The value to be written.


Example

See "WRITE Procedure Signature 1".

WRITE Procedure Signature 6

This procedure writes an array attribute of type sys.xmltype. The procedure uses a XSL transformation to generate JSON. To determine the JSON type of values, it uses the following rules:

  • If the value is empty, it generates a NULL value.

  • If upper(value) is TRUE, it generates a boolean true value.

  • If upper(value) is FALSE, it generates a boolean false value.

  • If the XPath number function returns TRUE, it emits the value as is. Otherwise, it enquotes the value (that is, treats it as a JSON string).

Syntax

APEX_JSON.WRITE (
    p_value    IN sys.xmltype );

Parameters

Table 15-40 WRITE Procedure Parameters

Parameter Description

p_value

The value to be written.


Example

See "WRITE Procedure Signature 1".

WRITE Procedure Signature 7

This procedure writes an array with all rows that the cursor returns. Each row is a separate object. If the query contains object type, collection, or cursor columns, the procedure uses write(xmltype) to generate JSON. Otherwise, it uses DBMS_SQL to fetch rows and the write() procedures for the appropriate column data types for output. If the column type is varchar2 and the uppercase value is 'TRUE' or 'FALSE', it generates boolean values.

Syntax

APEX_JSON.WRITE (
    p_cursor      IN OUT NOCOPY sys_refcursor );

Parameters

Table 15-41 WRITE Procedure Parameters

Parameter Description

p_cursor

The cursor.


Example 1

This example writes an array containing JSON objects for departments 10 and 20.

DECLARE
    c sys_refcursor;
BEGIN
    open c for select deptno, dname, loc from dept where deptno in (10, 20);
    apex_json.write(c);
END;

This is the output:

[ { "DEPTNO":10 ,"DNAME":"ACCOUNTING" ,"LOC":"NEW YORK" }
, { "DEPTNO":20 ,"DNAME":"RESEARCH" ,"LOC":"DALLAS" } ]

WRITE Procedure Signature 8

This procedure writes an object attribute of type VARCHAR2.

Syntax

APEX_JSON.WRITE (
    p_name         IN VARCHAR2,
    p_value        IN VARCHAR2,
    p_write_null   IN BOOLEAN  DEFAULT FALSE );

Parameters

Table 15-42 WRITE Procedure Parameters

Parameter Description

p_name

The attribute name.

p_value

The attribute value to be written.

p_write_null

If true, write NULL values. If false (the default), do not write NULLs.


Example

This example writes an object with named member attributes of various types. The comments to the right of the statements show the output that they generate.

DECLARE
  l_clob clob := 'long text';
  l_xml sys.xmltype := sys.xmltype('<obj><foo>1</foo><bar>2</bar></obj>');
BEGIN
  apex_json.open_object; -- {
  apex_json.write('a1', 1); -- "a1": 1
  apex_json.write('a2', 'two'); -- ,"a2": "two"
  apex_json.write('a3', l_clob); -- ,"a3": "long text"
  apex_json.write('a4', false); -- ,"a4": false
  apex_json.write('a5', sysdate); -- ,"a5": "2014-05-05T05:36:08Z"
  apex_json.write('a6', l_xml); -- ,"a6": { "foo": 1, "bar": 2 }
  apex_json.close_object; -- }
END;

WRITE Procedure Signature 9

This procedure writes an object attribute of type CLOB.

Syntax

APEX_JSON.WRITE (
    p_name     IN VARCHAR2,
    p_value    IN CLOB,
    p_write_null   IN BOOLEAN  DEFAULT FALSE );

Parameters

Table 15-43 WRITE Procedure Parameters

Parameter Description

p_name

The attribute name.

p_value

The attribute value to be written.

p_write_null

If true, write NULL values. If false (the default), do not write NULLs.


Example

See example for "WRITE Procedure Signature 8".

WRITE Procedure Signature 10

This procedure writes an object attribute of type NUMBER.

Syntax

APEX_JSON.WRITE (
    p_name     IN VARCHAR2,
    p_value    IN NUMBER,
    p_write_null   IN BOOLEAN  DEFAULT FALSE );

Parameters

Table 15-44 WRITE Procedure Parameters

Parameter Description

p_name

The attribute name.

p_value

The attribute value to be written.

p_write_null

If true, write NULL values. If false (the default), do not write NULLs.


Example

See example for "WRITE Procedure Signature 8".

WRITE Procedure Signature 11

This procedure writes an object attribute of type date.

Syntax

APEX_JSON.WRITE (
    p_name     IN VARCHAR2,
    p_value    IN DATE,
    p_format   IN VARCHAR2 DEFAULT c_date_iso8691,
    p_write_null   IN BOOLEAN  DEFAULT FALSE );

Parameters

Table 15-45 WRITE Procedure Parameters

Parameter Description

p_name

The attribute name.

p_value

The attribute value to be written.

p_format

The date format mask (default wwv_flow_json.c_date_iso8601.

p_write_null

If true, write NULL values. If false (the default), do not write NULLs.


Example

See example for "WRITE Procedure Signature 8".

WRITE Procedure Signature 12

This procedure writes an object attribute of type boolean.

Syntax

APEX_JSON.WRITE (
    p_name     IN VARCHAR2,
    p_value    IN BOOLEAN,
    p_write_null   IN BOOLEAN  DEFAULT FALSE );

Parameters

Table 15-46 WRITE Procedure Parameters

Parameter Description

p_name

The attribute name.

p_value

The attribute value to be written.

p_write_null

If true, write NULL values. If false (the default), do not write NULLs.


Example

See example for "WRITE Procedure Signature 8".

WRITE Procedure Signature 13

This procedure writes an attribute where the value is an array that contains all rows that the cursor returns. Each row is a separate object.

If the query contains object type, collection, or cursor columns, the procedure uses write(p_name,<xmltype>). See "WRITE Procedure Signature 14." Otherwise, it uses DBMS_SQL to fetch rows and the write() procedures for the appropriate column data types for output. If the column type is varchar2 and the uppercase value is 'TRUE' or 'FALSE', it generates boolean values.

Syntax

APEX_JSON.WRITE (
    p_name        IN VARCHAR2,
    p_cursor      IN OUT NOCOPY sys_refcursor );
 

Parameters

Table 15-47 WRITE Procedure Parameters

Parameter Description

p_name

The attribute name.

p_cursor

The cursor.


Example

This example writes an array containing JSON objects for departments 10 and 20, as an object member attribute.

DECLARE
  c sys_refcursor;
BEGIN
  open c for select deptno,
                    dname,
                    cursor(select empno,
                                  ename
                             from emp e
                            where e.deptno=d.deptno) emps
               from dept d;
  apex_json.open_object;
  apex_json. write('departments', c);
  apex_json.close_object;
END;
 
{ "departments":[
      {"DEPTNO":10,
       "DNAME":"ACCOUNTING",
       "EMPS":[{"EMPNO":7839,"ENAME":"KING"}]},
      ...
     ,{"DEPTNO":40,"DNAME":"OPERATIONS","EMPS":null}] }

WRITE Procedure Signature 14

This procedure writes an array attribute of type sys.xmltype. The procedure uses a XSL transformation to generate JSON. To determine the JSON type of values, it uses the following rules:

  • If the value is empty, it generates a NULL value.

  • If upper(value) is TRUE, it generates a boolean true value.

  • If upper(value) is FALSE, it generates a boolean false value.

  • If the XPath number function returns true, it emits the value as is. Otherwise, it enquotes the value (that is, treats it as a JSON string).

Syntax

APEX_JSON.WRITE (
    p_name     IN VARCHAR2,
    p_value    IN sys.xmltype,
    p_write_null   IN BOOLEAN  DEFAULT FALSE );

Parameters

Table 15-48 WRITE Procedure Parameters

Parameter Description

p_name

The attribute name.

p_value

The value to be written. The XML is converted to JSON

p_write_null

If true, write NULL values. If false (the default), do not write NULLs.


Example

See example for "WRITE Procedure Signature 13."

WRITE Procedure Signature 15

This procedure writes parts of a parsed APEX_JSON.t_values table.

Syntax

APEX_JSON.WRITE (
    p_values           IN t_values,
    p_path             IN VARCHAR2 DEFAULT '.',
    p0                 IN VARCHAR2 DEFAULT NULL,
    p1                 IN VARCHAR2 DEFAULT NULL,
    p2                 IN VARCHAR2 DEFAULT NULL,
    p3                 IN VARCHAR2 DEFAULT NULL,
    p4                 IN VARCHAR2 DEFAULT NULL );

Parameters

Table 15-49 WRITE Procedure Parameters

Parameter Description

p_values

The parsed JSON members.

p_path

The index into p_values.

p[0-4]

Each %N in p_path will be replaced by pN and every i-th %s or %d is replaced by p[i-1].


Example

This example parses a JSON string and writes parts of it.

DECLARE
  j apex_json.t_values;
BEGIN
  apex_json.parse(j, '{ "foo": 3, "bar": { "x": 1, "y": 2 }}');
  apex_json.write(j,'bar');
END;

WRITE Procedure Signature 16

This procedure writes parts of a parsed APEX_JSON.t_values table as an object member attribute.

Syntax

APEX_JSON.WRITE (
    p_name             IN VARCHAR2,
    p_values           IN t_values,
    p_path             IN VARCHAR2 DEFAULT '.',
    p0                 IN VARCHAR2 DEFAULT NULL,
    p1                 IN VARCHAR2 DEFAULT NULL,
    p2                 IN VARCHAR2 DEFAULT NULL,
    p3                 IN VARCHAR2 DEFAULT NULL,
    p4                 IN VARCHAR2 DEFAULT NULL,
    p_write_null       IN BOOLEAN  DEFAULT FALSE ); 

Parameters

Table 15-50 WRITE Procedure Parameters

Parameter Description

p_name

The attribute name.

p_values

The parsed JSON members.

p_path

The index into p_values.

p[0-4]

Each %N in p_path will be replaced by pN and every i-th %s or %d is replaced by p[i-1].

p_write_null

If true, write NULL values. If false (the default), do not write NULLs.


Example

This example parses a JSON string and writes parts of it as an object member.

DECLARE
  j apex_json.t_values;
BEGIN
  apex_json.parse(j, '{ "foo": 3, "bar": { "x": 1, "y": 2 }}');
  apex_json.open_object; -- {
  apex_json.write('parsed-bar',j,'bar');-- "parsed-bar":{ "x":1 ,"y":2 }
  apex_json.close_object; -- }
END;