The code looks like this:
SELECT
JSON_OBJECT(
ROOM_LIGHT VALUE
JSON_OBJECT
(
'START' VALUE START,
'ENDE' VALUE ENDE,
'RUN' VALUE RUNNING
)
)
FROM runtime
WHERE 1 = 1
GROUP BY ROOM_LIGHT;
I want this JSON back:
{
"ROOM_LIGHT": "01": [
{
"START" :"",
"ENDE" :"",
"RUNNING": ""
},
{
"START" :"",
"ENDE" :"",
"RUNNING": ""
}
],
"ROOM_LIGHT": "02": [
{
"START" :"",
"ENDE" :"",
"RUNNING": ""
},
{
"START" :"",
"ENDE" :"",
"RUNNING": ""
}
]
}
My problem is the group by doesn’t work. I get this kind of JSON back:
{ "ROOM_LIGHT 01": {"START": "", "ENDE":"", "RUNNING":""}},
{ "ROOM_LIGHT 01": {"START": "", "ENDE":"", "RUNNING":""}},
{ "ROOM_LIGHT 01": {"START": "", "ENDE":"", "RUNNING":""}}
2
Answers
Your expected output is invalid JSON as you have multiple keys in the same object with the same
ROOM_LIGHT
key.Use
JSON_ARRAYAGG
:Which, for the sample data:
Note:
START
is a reserved word and cannot be used as an identifier unless you use a quoted identifier.Outputs:
fiddle
Update
If you are having problems with the size of the string then use
RETURNING CLOB
to return aCLOB
, instead of aVARCHAR2
:fiddle
You will have to use
JSON_ARRAYAGG
to group elements into one array :Result :
Demo here