I am trying to parse a large json using JSON_OBJECT_T
, JSON_ARRAY_T
APIs and its working fine but I want expert suggestions whether it is efficient or not?
I am adding my json file and parsing code as below
Code
SET SERVEROUTPUT ON;
DECLARE
l_clob clob;
l_time timestamp;
l_json json_object_t;
l_stops_array json_array_t;
l_stops_arr json_array_t;
routeInfoObj json_object_t;
routeStopArr json_array_t;
BEGIN
SELECT LOG_CLOB INTO l_clob FROM ITV_DEV_LOGS WHERE LOG_ID = 1435334;
l_time := systimestamp;
l_json := json_object_t.parse( l_clob );
dbms_output.put_line( 'Parsing Time: ' || extract(second from( systimestamp - l_time ) ) );
l_stops_array := l_json.get_array('data');
DBMS_OUTPUT.PUT_LINE('Data array: '||l_stops_array.get_size);
FOR i in 0..l_stops_array.get_size-1 loop
l_stops_arr := TREAT(l_stops_array.get(i) AS JSON_OBJECT_T).get_array('routedStops');
DBMS_OUTPUT.PUT_LINE('stops array: '||l_stops_arr.get_size);
FOR j in 0..l_stops_arr.get_size - 1 loop
routeInfoObj := TREAT(l_stops_arr.get(j) AS JSON_OBJECT_T).get_object('routingInfo');
DBMS_OUTPUT.PUT_LINE('Stop : ' || routeInfoObj.get_number('stop'));
routeStopArr := TREAT(l_stops_arr.get(j) AS JSON_OBJECT_T).get_array('routedJobs');
FOR k in 0..routeStopArr.get_size - 1 loop
DBMS_OUTPUT.PUT_LINE('JobRef : ' || TREAT(routeStopArr.get(k) AS JSON_OBJECT_T).get_string('jobRef'));
// update query to update stop value to respective jobRef
end loop;
end loop;
end loop;
END;
It's working fine but is there a way to improve this implementation as this is just a sample json and count of objects inside may go to 2000 and instead of updating records one by one, is there a way to update all records in one statement?
You can use
json_table()
to turn the JSON value into a relational representation. That in turn can be used within a MERGE statement.E.g. the following query:
Returns something like this:
That could be used as the source of a MERGE statement to update your target table:
As you neither provided the structure of the target nor the information which JSON keys should be matched to which table columns, all column names are just guesses and you need to replace them with the correct names.