DECLARE
l_object json_object_t;
BEGIN
l_object := json_object_t.parse ('{"Name":"Elvis"}');
DBMS_OUTPUT.put_line (l_object.get_string ('Name'));
END;
DECLARE
l_object json_object_t;
l_key_list json_key_list;
BEGIN
-- JSON_OBJECT can figure out what keys it has...
l_object := json_object_t.parse (
'{"Name":"Elvis",
"Address":"Elsewhere",
"Numbers":[1,2,3,4,5],
"Stuff":[{"A":"B"},{"B":"C"}]}');
l_key_list := l_object.get_keys;
FOR counter IN 1 .. l_key_list.COUNT
LOOP
DBMS_OUTPUT.put_line (
l_key_list (counter)
|| ' = '
|| l_object.get_string (l_key_list (counter)));
END LOOP;
END;
DECLARE
l_object json_object_t;
BEGIN
-- JSON_OBJECT_T stores JSON data of all types
-- must be key-value pairs though
l_object := json_object_t.parse ('{"Name":"Elvis"}');
l_object := json_object_t.parse ('{"Numbers":[1,2,3,4,5]}');
l_object := json_object_t.parse ('{"Stuff":[{"A":"B"},{"B":"C"}]}');
END;
DECLARE
l_object json_object_t;
BEGIN
-- The JSON must be valid though...
l_object := json_object_t.parse ('Not JSON At All');
END;
DECLARE
l_object json_object_t;
BEGIN
-- must be key-value pair; not just an alement
l_object := json_object_t.parse ('[0,1,2,3,4,5]');
END;
DECLARE
l_object json_object_t;
PROCEDURE what_am_i (p_id NUMBER)
IS
BEGIN
DBMS_OUTPUT.put_line ('#' || p_id);
DBMS_OUTPUT.put_line ('Size ' || l_object.get_size);
IF l_object.is_object
THEN
DBMS_OUTPUT.put_line ('OBJECT');
END IF;
END;
BEGIN
-- It can interrogate itself...
l_object := json_object_t.parse ('{"Name":"Elvis"}');
what_am_i (1);
l_object := json_object_t.parse ('{"Numbers":[1,2,3,4,5]}');
what_am_i (2);
l_object := json_object_t.parse ('{"Stuff":[{"A":"B"},{"B":"C"}]}');
what_am_i (3);
END;
DECLARE
l_object json_object_t;
PROCEDURE output (p_key VARCHAR2)
IS
BEGIN
DBMS_OUTPUT.put_line (p_key);
-- if single value (scalar)...
IF l_object.get_type (p_key) = 'SCALAR'
THEN
IF l_object.is_string
THEN
DBMS_OUTPUT.put_line ('Elem = ' || l_object.to_string);
END IF;
DBMS_OUTPUT.put_line ('Obj = ' || l_object.get_string (p_key));
-- An array
ELSIF l_object.get_type (p_key) = 'ARRAY'
THEN
DBMS_OUTPUT.put_line ('Array!');
ELSE -- not scalar
DBMS_OUTPUT.put_line ('Not A SCALAR or String');
END IF; -- scalar?
END;
BEGIN
-- Or the code can check...
l_object := json_object_t.parse ('{"Name":"Elvis"}');
output ('Name');
l_object := json_object_t.parse ('{"Numbers":[1,2,3,4,5]}');
output ('Numbers');
l_object := json_object_t.parse ('{"Stuff":[{"A":"B"},{"B":"C"}]}');
output ('Stuff');
END;
DECLARE
l_object json_object_t;
PROCEDURE process_it (p_key VARCHAR2)
IS
l_e json_element_t;
BEGIN
-- elements can be extracted from JSON_OBJECT_T
l_e := l_object.get (p_key);
DBMS_OUTPUT.put_line (l_e.to_string);
END;
BEGIN
l_object := json_object_t.parse ('{"Name":"Elvis"}');
process_it ('Name');
l_object := json_object_t.parse ('{"Numbers":[1,2,3,4,5]}');
process_it ('Numbers');
l_object := json_object_t.parse ('{"Stuff":[{"A":"B"},{"B":"C"}]}');
process_it ('Stuff');
END;
DECLARE
-- objects can be arrays
l_object json_object_t;
PROCEDURE process_it (p_key VARCHAR2)
IS
l_e json_element_t;
l_a json_array_t;
BEGIN
l_e := l_object.get (p_key);
IF l_e.is_array
THEN
l_a := json_array_t.parse (l_e.to_string);
DBMS_OUTPUT.put_line (
p_key || ' is an array of size ' || l_a.get_size);
ELSE
DBMS_OUTPUT.put_line (p_key || ' is not an array');
END IF;
END;
BEGIN
l_object := json_object_t.parse ('{"Name":"Elvis"}');
process_it ('Name');
l_object := json_object_t.parse ('{"Numbers":[1,2,3,4,5]}');
process_it ('Numbers');
l_object := json_object_t.parse ('{"Stuff":[{"A":"B"},{"B":"C"}]}');
process_it ('Stuff');
END;
DECLARE
l_object json_object_t;
BEGIN
-- Elements can be added to an object...
-- ...conditionally...
-- ...overloaded...
-- ...and removed...
-- ...or renamed...
l_object := json_object_t.parse ('{"Name":"Elvis"}');
l_object.put ('Nickname', 'The King');
DBMS_OUTPUT.put_line (l_object.to_string);
-- PUT is an up-sert!
l_object.put ('Nickname', 'Not The King');
DBMS_OUTPUT.put_line (l_object.to_string);
l_object.put ('Age', 11);
DBMS_OUTPUT.put_line (l_object.to_string);
IF NOT l_object.has ('Age')
THEN
l_object.put ('Age', 22);
END IF;
l_object.remove ('Age');
DBMS_OUTPUT.put_line (l_object.to_string);
l_object.rename_key ('Nickname', 'RealityIs');
DBMS_OUTPUT.put_line (l_object.to_string);
END;
DECLARE
l_object json_object_t;
BEGIN
-- remove is forgiving
l_object := json_object_t.parse ('{"Name":"Elvis"}');
l_object.remove ('NotThere');
DBMS_OUTPUT.put_line (l_object.to_string);
END;
DECLARE
l_object json_object_t;
BEGIN
-- rename is not forgiving
l_object := json_object_t.parse ('{"Name":"Elvis"}');
l_object.rename_key ('NotThere', 'DudeItsNotThere');
DBMS_OUTPUT.put_line (l_object.to_string);
END;
DECLARE
l_object json_object_t;
e_singing_off_key EXCEPTION;
PRAGMA EXCEPTION_INIT (e_singing_off_key, -40571);
BEGIN
-- plsql to the rescue!
l_object := json_object_t.parse ('{"Name":"Elvis"}');
BEGIN
l_object.rename_key ('NotThere', 'DudeItsNotThere');
EXCEPTION
WHEN e_singing_off_key
THEN
NULL;
WHEN OTHERS
THEN
RAISE;
END;
DBMS_OUTPUT.put_line (l_object.to_string);
END;