Skip to content
Advertisement

Returning Json string from oracle procedure

I need to have a stored procedure in PL/SQL that return a string containing a json object. This object must be a list of object representing rows a table.

For example I have the table MY_TABLE(ID, TB_VALUE)

The output must be :

My stored procedure looks like that :

What’s the best way to achieve this ? Should I use DBMS_OUTPUT.PUT_LINE ?

Advertisement

Answer

From Oracle 12, you do not need a procedure or to create your own JSON functions.

Outputs:

JSON
[{“id”:1,”value”:”valueTest”},{“id”:2,”value”:”valueTest2″},{“id”:3,”value”:”valueTest3″}]

If you really want it in a package then convert it to a function and just wrap the query:

Then:

Outputs:

PKG_GETCOR.SELECTFUNC()
[{“id”:1,”value”:”valueTest”},{“id”:2,”value”:”valueTest2″},{“id”:3,”value”:”valueTest3″}]

db<>fiddle here

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement