I have a PL/SQL handler using the SODA package to manipulate a JSON database. I want to:
- Read the value for key
idin the payload - Write the payload JSON into a new document in the database.
To do step 1, The handler takes :body to be parsed as a JSON_OBJECT_T type, so that I can access the value for key id in the payload.
But for step 2, if I read body again when constructing with SODA_DOCUMENT_T(b_content=> :body), it will simply give me a blank document.
I also can't pass in the JSON_OBJECT_T variable in directly, like SODA_DOCUMENT_T(j_content=> jso), because that function expects a JSON type and not a JSON_OBJECT_T. I can't find the JSON type documentation, but saw in code examples the function JSON('{}') to generate one.
Reading :body_text however gives me other problems - because JSON() function cannot handle line breaks in the payload and gives an error instead.
Currently to work around this I'm using the following:
SODA_DOCUMENT_T(
j_content => JSON(jso.to_string())
)
Which seems very silly because I'm serialising it to a string again before converting it back into JSON type. Is there a proper way to read the value of a key of the payload, and pass it into the SODA_DOCUMENT_T?
Yes, you can't pass in
JSON_Object_Tinstance toSODA_Document_Tconstructor. However, you may useJSON_QUERY()PL/SQL function that drills into the JSON document given a path expression and it returns a JSON type instance.Example:
In the above example,
$.idis the path expression andjson_query()returns json value corresponding to the fieldidstarting from the root$Documentation: https://docs.oracle.com/en/database/oracle/oracle-database/21/adjsn/function-JSON_QUERY.html
You should now be able to pass this
jvalinstance of typeJSONtoSODA_Document_Tconstructor.Yes, I think you should definitely avoid back and forth conversions.
Usage:
I recommend this approach. Let me know if that helps!
Alternate solution:
BTW, given a DOM, you can directly go to JSON type instance as well, using the following method in
JSON_Object_Ttype:This method pretty much does the same work as
JSON_Query()shown above: