1
declare
       l_json_doc VARCHAR2(32767); 
       l_numcols number;
       l_numrows number;      
begin

l_json_doc := '{
    "table": {
        "name": "sometablename",
        "numofcolumns": 5,
        "numofrows": 5,
        "colheadings": [{
                "colname": "customcol1",
                "coltype": "number"
            },
            {
                "colname": "customcol2",
                "coltype": "varchar2"
            },
            {
                "colname": "customcol3",
                "coltype": "varchar2"
            },
            {
                "colname": "customcol4",
                "coltype": "varchar2"
            },
            {
                "colname": "customcol5",
                "coltype": "number"
            }
        ],
        "data": [{
                "customcol1": "datacolumn1",
                "customcol2": "datacolumn2",
                "customcol3": "datacolumn3",
                "customcol4": "datacolumn4",
                "customcol5": "datacolumn5"
            },
            {
                "customcol1": "2datacolumn1",
                "customcol2": "2datacolumn2",
                "customcol3": "2datacolumn3",
                "customcol4": "2datacolumn4",
                "customcol5": "2datacolumn5"
            },
            {
                "customcol1": "3datacolumn1",
                "customcol2": "3datacolumn2",
                "customcol3": "3datacolumn3",
                "customcol4": "3datacolumn4",
                "customcol5": "3datacolumn5"
            },
            {
                "customcol1": "4datacolumn1",
                "customcol2": "4datacolumn2",
                "customcol3": "4datacolumn3",
                "customcol4": "4datacolumn4",
                "customcol5": "4datacolumn5"
            }
        ]
    }
}';

APEX_JSON.parse(l_json_doc);

l_numcols := APEX_JSON.get_count(p_path => 'table.colheadings');

l_numrows := APEX_JSON.get_count(p_path => 'table.data');

FOR i IN 1 .. l_numrows LOOP
FOR j IN 1 .. l_numcols LOOP

dbms_output.put_line('TEST ' || APEX_JSON.get_varchar2(p_path => 'table.data[%d]')  ); 

END LOOP;
END LOOP;


end;

This is my code which is supposed to extract the data array objects. I expect the following output:

TEST {"customcol1": "datacolumn1","customcol2": "datacolumn2","customcol3": "datacolumn3","customcol4": "datacolumn4","customcol5": "datacolumn5"}

TEST { "customcol1": "2datacolumn1","customcol2": "2datacolumn2","customcol3": "2datacolumn3","customcol4": "2datacolumn4","customcol5": "2datacolumn5"}

etc ...

But when I try to get the json object from the data array using the APEX_JSON.get_varchar2 it returns empty

1 Answer 1

1

Two problems with the code there

  • You have not provided an index to the get_varchar2 function. So the path is not complete. You need to provide a value in the p0 parameter of that function
  • That's not how APEX_JSON.get_varchar2 works. You are expecting the function to return the entire JSON object in the data array but get_varchar2 cannot do that. It can only get you the VALUE that is a VARCHAR at a specified path. It cannot get you the entire object.

For your output

DECLARE
   l_json_doc   VARCHAR2 (32767);
   l_numcols    NUMBER;
   l_numrows    NUMBER;
   v_colname    VARCHAR2 (32767);
BEGIN
   l_json_doc := '{
    "table": {
        "name": "sometablename",
        "numofcolumns": 5,
        "numofrows": 5,
        "colheadings": [{
                "colname": "customcol1",
                "coltype": "number"
            },
            {
                "colname": "customcol2",
                "coltype": "varchar2"
            },
            {
                "colname": "customcol3",
                "coltype": "varchar2"
            },
            {
                "colname": "customcol4",
                "coltype": "varchar2"
            },
            {
                "colname": "customcol5",
                "coltype": "number"
            }
        ],
        "data": [{
                "customcol1": "datacolumn1",
                "customcol2": "datacolumn2",
                "customcol3": "datacolumn3",
                "customcol4": "datacolumn4",
                "customcol5": "datacolumn5"
            },
            {
                "customcol1": "2datacolumn1",
                "customcol2": "2datacolumn2",
                "customcol3": "2datacolumn3",
                "customcol4": "2datacolumn4",
                "customcol5": "2datacolumn5"
            },
            {
                "customcol1": "3datacolumn1",
                "customcol2": "3datacolumn2",
                "customcol3": "3datacolumn3",
                "customcol4": "3datacolumn4",
                "customcol5": "3datacolumn5"
            },
            {
                "customcol1": "4datacolumn1",
                "customcol2": "4datacolumn2",
                "customcol3": "4datacolumn3",
                "customcol4": "4datacolumn4",
                "customcol5": "4datacolumn5"
            }
        ]
    }
}';

   APEX_JSON.parse (l_json_doc);

   l_numcols := APEX_JSON.get_count (p_path => 'table.colheadings');

   l_numrows := APEX_JSON.get_count (p_path => 'table.data');

   FOR i IN 1 .. l_numrows
   LOOP
      DBMS_OUTPUT.put ('TEST {');

      FOR j IN 1 .. l_numcols
      LOOP
         v_colname :=
            apex_json.get_varchar2 ('table.colheadings[%d].colname', j);
         DBMS_OUTPUT.put (
            '"' || v_colname || '":"'
            || APEX_JSON.get_varchar2 (
                  p_path   => 'table.data[%d].' || v_colname,
                  p0       => i)
            || '",');
      END LOOP;

      DBMS_OUTPUT.put_line ('}');
   END LOOP;
END;

Here is the output of the code:

TEST {"customcol1":"datacolumn1","customcol2":"datacolumn2","customcol3":"datacolumn3","customcol4":"datacolumn4","customcol5":"datacolumn5",}
TEST {"customcol1":"2datacolumn1","customcol2":"2datacolumn2","customcol3":"2datacolumn3","customcol4":"2datacolumn4","customcol5":"2datacolumn5",}
TEST {"customcol1":"3datacolumn1","customcol2":"3datacolumn2","customcol3":"3datacolumn3","customcol4":"3datacolumn4","customcol5":"3datacolumn5",}
TEST {"customcol1":"4datacolumn1","customcol2":"4datacolumn2","customcol3":"4datacolumn3","customcol4":"4datacolumn4","customcol5":"4datacolumn5",}

NOTE: I didn't care to remove the final comma after the last key:value pair in each row. If you want that then you have to store all the key value pairs in a variable and RTRIM the comma out.

Sign up to request clarification or add additional context in comments.

1 Comment

Is it somehow possible to get a sub-object with apex_json? So with some kind of apex_json.get_object or so?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.