在Oracle中格式化JSON(列出键后跟着值集)?

of1yzvn4  于 2023-08-08  发布在  Oracle
关注(0)|答案(1)|浏览(133)

我想看看是否有可能在Oracle中将JSON格式化为头列表,然后列出所有值集。
创建以下内容(在Oracle 19 c上):

CREATE TABLE tbl1 (val1 varchar2(10), val2 varchar2(10), val3 varchar2(10));
INSERT INTO tbl1 VALUES ('1','2','3');
INSERT INTO tbl1 VALUES ('a','b','c');
INSERT INTO tbl1 VALUES ('1','b','3');

字符串
运行此查询可得到:

SELECT JSON_ARRAYAGG(JSON_OBJECT (*) returning varchar2) AS JSON_VAR
FROM tbl1;


| JSON_VAR |
| ------------ |
| [{"VAL1":"1","VAL2":"2","VAL3":"3"},{"VAL1":"a","VAL2":"b","VAL3":"c"},{"VAL1":"1","VAL2":"b","VAL3":"3"}] |
是否可以格式化输出,其中我们列出一次键,然后列出所有的值集?举例来说:

{"keys" : ["VAL1", "VAL2", "VAL3"], "values" : [["1", "2", "3"],["a", "b", "c"],["1","b","c"]]}


fiddle

fykwrbwg

fykwrbwg1#

select json_object(
'keys' : ['VAL1', 'VAL2', 'VAL3'],
'values' : json_arrayagg(json_array(val1, val2, val3))) as js
from tbl1

字符串

相关问题