I have JSON array inside varchar
DECLARE JsonArray varchar2(1000); arrayCars varchar2(1000); BEGIN JsonArray :={"Cars": [{"name":"Honda", "color":"red" }, {"name":"Toyota", "color":"green"}] } SELECT JSON_QUERY(JsonArray, '$.Cars') into arrayCars FROM dual; END; /
Now if I print out arrayCars i will get
[{“name”:”Honda”,”color”:”red”},{“name”:”Toyota”,”color”:”green”}]
But how can I loop through this Cars array and print out it’s components seperately (get access to them)?
Advertisement
Answer
You can directly use SQL
with JSON_TABLE()
function which is available starting with Oracle DB 12.1.0.2
version such as
WITH t(arrayCars) AS ( SELECT JSON_QUERY('{"Cars": [{"name":"Honda", "color":"red" }, {"name":"Toyota", "color":"green"}] }', '$.Cars') FROM dual ) SELECT name, color FROM t CROSS JOIN JSON_TABLE(arrayCars, '$' COLUMNS(NESTED PATH '$[*]' COLUMNS( name VARCHAR2(100) PATH '$.name', color VARCHAR2(100) PATH '$.color' ) ) );
If you really need to use PL/SQL
, then consider creating a function with SYS_REFCURSOR
return type such as
CREATE OR REPLACE FUNCTION Get_Cars RETURN SYS_REFCURSOR IS v_recordset SYS_REFCURSOR; JsonArray VARCHAR2(1000); arrayCars VARCHAR2(1000); v_sql VARCHAR2(32767); BEGIN JsonArray :='{"Cars": [{"name":"Honda", "color":"red" }, {"name":"Toyota", "color":"green"}] }'; arrayCars := JSON_QUERY(JsonArray, '$.Cars'); DBMS_OUTPUT.PUT_LINE(arrayCars); v_sql := 'SELECT name,color FROM dual CROSS JOIN JSON_TABLE(:Cars, ''$'' COLUMNS(NESTED PATH ''$[*]'' COLUMNS( name VARCHAR2(100) PATH ''$.name'', color VARCHAR2(100) PATH ''$.color'' ) ) )'; OPEN v_recordset FOR v_sql USING arrayCars; RETURN v_recordset; END; /
and then call from SQL Developer’s console as
SQL> DECLARE result SYS_REFCURSOR; BEGIN :result := Get_Cars; END; / SQL> PRINT result ;
Edit(for your last comment):
Alternatively, you can use a simple implicit loop such as
SQL> SET SERVEROUTPUT ON SQL> DECLARE v_name VARCHAR2(1000); v_color VARCHAR2(1000); BEGIN FOR c IN ( SELECT name,color FROM JSON_TABLE('{"Cars": [{"name":"Honda", "color":"red" }, {"name":"Toyota", "color":"green"}] }', '$' COLUMNS(NESTED PATH '$.Cars[*]' COLUMNS( name VARCHAR2(100) PATH '$.name', color VARCHAR2(100) PATH '$.color' ) ) ) ) LOOP v_name := c.name; v_color := c.color; DBMS_OUTPUT.PUT_LINE(v_name||' '||v_color); END LOOP; END; /