Skip to Main Content

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Mohamed.

Asked: November 28, 2019 - 9:35 am UTC

Last updated: November 28, 2019 - 4:52 pm UTC

Version: 18c

Viewed 1000+ times

You Asked

I am currently trying to generate some REST services in Oracle 18c database and one of my requirement is to send the input payload along with the response. But while attaching the input some additional '/' is also getting added along with each " as an escape character. Could you please help me get rid of the same.

Sample code:


DECLARE
    p   CLOB := '{"name": "John","age" : 29}'; --Input
    j   json_object_t := json_object_t.parse('{"Response":"Success"}'); --output
BEGIN
    j.put('INPUT', p);  --attaching input to response
    dbms_output.put_line(j.stringify); -- this has additional / in the o/p.
END;


and Chris said...

That's because you're adding an attribute with the string value "{"name": "John","age" : 29}". Not the JSON object!

If you want to add an object, you need to convert the string to a json_object_t first. THEN add it to the other document:

DECLARE
    p   clob := '{"name": "John","age" : 29}'; --Input
    j   json_object_t := json_object_t.parse('{"Response":"Success"}'); --output
    j2  json_object_t := json_object_t.parse(p); 
BEGIN
    j.put('INPUT1', j2); 
    j.put('INPUT2', p);  
    dbms_output.put_line(j.stringify); -- this has additional / in the o/p.
END;
/

{
 "Response":"Success",
 "INPUT1":{"name":"John","age":29},
 "INPUT2":"{\"name\": \"John\",\"age\" : 29}"
}


Is this answer out of date? If it is, please let us know via a Comment

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library