I'm trying to convert a record type, defined in a PL/SQL package, to JSON.
I found that in SQL, I can use select json_object(*) from SomeTable, to return objects that have a property for each column in the table, but I can't seem to do this with record types in PL/SQL code.
Example package with types and a function to return (serialize) json based on a type:
create or replace package Customer as
type RT_Address is record (
Line1 varchar2(100),
Line2 varchar2(100),
City varchar2(100)
);
type RT_Customer is record (
FirstName varchar2(100),
LastName varchar2(100),
Address RT_Address
);
function AsJson(P_Customer RT_Customer)
return varchar2;
end;
create or replace package body devvanessa.Customer as
function AsJson(P_Customer RT_Customer)
return varchar2 is
V_DOM jdom_t;
V_JSON json_object_t;
V_JSONBody varchar2(4000);
begin
V_JSON := json_object(P_Customer); -- PLS-00103: Encountered the symbol when expecting one of the following: . ( * @ % & - + / at mod remainder rem <een exponent (**)> || multiset value
if V_DOM.append(P_CUSTOMER) then -- PLS-00306: wrong number or types of arguments
null;
end if;
V_JSONBody := V_Json.STRINGIFY;
return V_JSONBody;
end;
end;
The above is slightly simplified, because I actually want to store this json and do some other things with it, but it does show the core of my problem:
How do I convert a record type to Json in PL/SQL, without specifying all individual fields separately. I'm also curious how it would work the other way around.
I've been searching various sources, like documentation on JSON functions, Oracle 19's JSON documentation, and simply the code completion hints I got on the json_object_t and jdom_t types, but so far I can't find any evidence that it's possible at all.
This would work:
V_JSon.Put('FirstName', P_Customer.FirstName);
-- repeated for each property
This way I get json, but it requires me to specify each field individually.