I need to parse JSON to a table or array. As this is completely new for me I've used Google for help but something is not working for me :/
My JSON looks like:
{
"statusCode": 200,
"isValid": true,
"errors": [],
"result": {
"optimizationData": [
{
"name": "out",
"content": "ID_LIMIT;TIME_STAMP_FROM;DIRECTION;ID_MODEL_CONSTRAINT\n1;202109222200;G;2_7_1_G\n1;202109232200;G;2_3_1_G\n2;202109222200;G;2_3_1_G\n3;202109222200;G;3_3_1_P\n"
},
{
"name": "unit_out",
"content": "ID_LIMIT;CODE_UNIT;TIME_STAMP_FROM;TIME_STAMP_TO;VARIABLE;VALUE\n1;BEL 2-02;202109222200;202109232200;RelaxationPlus;10\n1;BEL 2-05;202109222200;202109232200;RelaxationPlus;10\n2;WLO 1-01;202109222200;202109232200;RelaxationMinus;10\n"
}
]
}
}
From above JSON I'd like to create table (or arrays) like below (example expected data for name: "unit_out"):
| ID_LIMIT | CODE_UNIT | TIME_STAMP_FROM | TIME_STAMP_TO | VARIABLE | VALUE |
|---|---|---|---|---|---|
| 1 | BEL 2-02 | 202109222200 | 202109232200 | RelaxationPlus | 10 |
| 1 | BEL 2-05 | 202109222200 | 202109232200 | RelaxationPlus | 10 |
| 2 | WLO 1-01 | 202109222200 | 202109232200 | RelaxationMinus | 10 |
So far I've tried to wrote a function to return "content" value, but I'm getting ORA-30625 (method dispatch on null self argument is disallowed)
CREATE OR REPLACE FUNCTION parse_json (json IN VARCHAR2)
RETURN VARCHAR2 IS
json_obj_in JSON_OBJECT_T;
json_arr JSON_ARRAY_T;
json_elem JSON_ELEMENT_T;
json_obj JSON_OBJECT_T;
name varchar2(32000);
content varchar2(32000);
BEGIN
json_obj_in := JSON_OBJECT_T.parse(json);
json_arr := json_obj_in.get_Array('result');
FOR i IN 0 .. json_arr.get_size - 1 --NVL(json_arr.FIRST, 1) .. NVL(json_arr.LAST, 0)
LOOP
json_obj := JSON_OBJECT_T(json_arr.get(i));
name := json_obj.get_Object('optimizationData').get_string('name');
content := json_obj.get_Object('optimizationData').get_string('content');
END LOOP;
RETURN json_obj_in.to_string;
END;
I'm not sure what's wrong or if my approach to this task is correct.
Can anyone help?
Thanks