Why do you want to get key names dynamically in JSON_QUERY
? That makes no sense.
You want to get key names dynamically. That is the problem statement. "In JSON_QUERY
" is your idea for how to get the keys. That idea is wrong.
But you can get the key names dynamically, and much more, with the JSON_DATAGUIDE
function. Then you can process the output further, if needed; just JSON_DATAGUIDE
itself gives you much more information: key names, but also data types and lengths - and it works for complex JSON strings too (with nested objects and arrays).
select json_dataguide('{a:100, b:200, c:300}')
from dual;
JSON_DATAGUIDE('{A:100,B:200,C:300}')
--------------------------------------------------------------------------------
[{"o:path":"$.a","type":"number","o:length":4},{"o:path":"$.b","type":"number","
o:length":4},{"o:path":"$.c","type":"number","o:length":4}]
Of course, if you just want an array of the top-level keys, you can further process this JSON array; I am not showing how, since you may be able to do that yourself (trivial manipulations), but do ask for help if needed.
Note that the above works in Oracle 12.2 and higher. Since Oracle 18 you can use additional options to format the output, if needed.