将JSON转换成oracle中JSON_VALUE可以读取的格式
Convert JSON in a format can be read by JSON_VALUE in oracle
我有 JSON 格式的数据 -
{"dims":{"ABC":{"dim":"ABC","bins":{"1290":{"bin":"1290","hits":"1","first_date":"03.11.15"},"345":{"bin":"345","hits":"2","first_date":"03.11.15"},"6603":{"bin":"6603","hits":"1","first_date":"23.12.15"},"6609":{"bin":"6609","hits":"1","first_date":"13.12.15"}}}}}
如何将其转换成格式-
{"dims":{"BINDUNGS_ID_CARDS":{"dim":"BINDUNGS_ID_CARDS","bins":[{"bin":"1290","hits":"1","first_date":"03.11.15"},{"bin":"345","hits":"2","first_date":"03.11.15"},{"bin":"6603","hits":"1","first_date":"23.12.15"},{"bin":"6609","hits":"1","first_date":"13.12.15"}]}}}
我想通过 JSON_VALUE(value, $.*.*.bins[0])
使用 JSON_VALUE 函数从 oracle 读取它。
我不知道如何通过 oracle 实现它。
您可以使用以下方法重新格式化它:
SELECT j.*
FROM table_name t
CROSS APPLY (
SELECT JSON_OBJECT(
'dims' VALUE JSON_OBJECT(
'BINDUNGS_ID_CARDS' VALUE JSON_OBJECT(
'dims' VALUE 'BINDUNGS_ID_CARDS',
'bins' VALUE JSON_ARRAYAGG(
data FORMAT JSON ORDER BY ROWNUM
)
)
)
) As value
FROM JSON_TABLE(
t.value,
'$.*.*.bins.*'
COLUMNS (
data CLOB FORMAT JSON PATH '$'
)
)
) j
其中,对于示例数据:
CREATE TABLE table_name ( value CLOB CHECK ( value IS JSON ) );
INSERT INTO table_name ( value )
VALUES (
'{
"dims":{
"ABC":{
"dim":"ABC",
"bins":{
"1290":{"bin":"1290","hits":"1","first_date":"03.11.15"},
"345":{"bin":"345","hits":"2","first_date":"03.11.15"},
"6603":{"bin":"6603","hits":"1","first_date":"23.12.15"},
"6609":{"bin":"6609","hits":"1","first_date":"13.12.15"}
}
}
}
}' );
输出:
| VALUE |
| :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| {"dims":{"BINDUNGS_ID_CARDS":{"dims":"BINDUNGS_ID_CARDS","bins":[{"bin":"1290","hits":"1","first_date":"03.11.15"},{"bin":"345","hits":"2","first_date":"03.11.15"},{"bin":"6603","hits":"1","first_date":"23.12.15"},{"bin":"6609","hits":"1","first_date":"13.12.15"}]}}} |
然而,这感觉像是一个 XY 问题。
I want to read it from oracle using JSON_VALUE function via JSON_VALUE(value, $.*.*.bins[0])
.
如果你想要对象的第一个值那么你不需要重新格式化 JSON 并且可以直接使用:
SELECT bin,
hits,
TO_DATE( first_date, 'DD.MM.RR' ) AS first_date
FROM table_name t
CROSS APPLY JSON_TABLE(
t.value,
'$.*.*.bins.*'
COLUMNS (
rn FOR ORDINALITY,
bin VARCHAR2(20) PATH '$.bin',
hits VARCHAR2(20) PATH '$.hits',
first_date VARCHAR2(8) PATH '$.first_date'
)
) j
WHERE rn = 1
输出:
BIN | HITS | FIRST_DATE
:--- | :--- | :------------------
1290 | 1 | 2015-11-03 00:00:00
db<>fiddle here
我有 JSON 格式的数据 -
{"dims":{"ABC":{"dim":"ABC","bins":{"1290":{"bin":"1290","hits":"1","first_date":"03.11.15"},"345":{"bin":"345","hits":"2","first_date":"03.11.15"},"6603":{"bin":"6603","hits":"1","first_date":"23.12.15"},"6609":{"bin":"6609","hits":"1","first_date":"13.12.15"}}}}}
如何将其转换成格式-
{"dims":{"BINDUNGS_ID_CARDS":{"dim":"BINDUNGS_ID_CARDS","bins":[{"bin":"1290","hits":"1","first_date":"03.11.15"},{"bin":"345","hits":"2","first_date":"03.11.15"},{"bin":"6603","hits":"1","first_date":"23.12.15"},{"bin":"6609","hits":"1","first_date":"13.12.15"}]}}}
我想通过 JSON_VALUE(value, $.*.*.bins[0])
使用 JSON_VALUE 函数从 oracle 读取它。
我不知道如何通过 oracle 实现它。
您可以使用以下方法重新格式化它:
SELECT j.*
FROM table_name t
CROSS APPLY (
SELECT JSON_OBJECT(
'dims' VALUE JSON_OBJECT(
'BINDUNGS_ID_CARDS' VALUE JSON_OBJECT(
'dims' VALUE 'BINDUNGS_ID_CARDS',
'bins' VALUE JSON_ARRAYAGG(
data FORMAT JSON ORDER BY ROWNUM
)
)
)
) As value
FROM JSON_TABLE(
t.value,
'$.*.*.bins.*'
COLUMNS (
data CLOB FORMAT JSON PATH '$'
)
)
) j
其中,对于示例数据:
CREATE TABLE table_name ( value CLOB CHECK ( value IS JSON ) );
INSERT INTO table_name ( value )
VALUES (
'{
"dims":{
"ABC":{
"dim":"ABC",
"bins":{
"1290":{"bin":"1290","hits":"1","first_date":"03.11.15"},
"345":{"bin":"345","hits":"2","first_date":"03.11.15"},
"6603":{"bin":"6603","hits":"1","first_date":"23.12.15"},
"6609":{"bin":"6609","hits":"1","first_date":"13.12.15"}
}
}
}
}' );
输出:
| VALUE | | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | {"dims":{"BINDUNGS_ID_CARDS":{"dims":"BINDUNGS_ID_CARDS","bins":[{"bin":"1290","hits":"1","first_date":"03.11.15"},{"bin":"345","hits":"2","first_date":"03.11.15"},{"bin":"6603","hits":"1","first_date":"23.12.15"},{"bin":"6609","hits":"1","first_date":"13.12.15"}]}}} |
然而,这感觉像是一个 XY 问题。
I want to read it from oracle using JSON_VALUE function via
JSON_VALUE(value, $.*.*.bins[0])
.
如果你想要对象的第一个值那么你不需要重新格式化 JSON 并且可以直接使用:
SELECT bin,
hits,
TO_DATE( first_date, 'DD.MM.RR' ) AS first_date
FROM table_name t
CROSS APPLY JSON_TABLE(
t.value,
'$.*.*.bins.*'
COLUMNS (
rn FOR ORDINALITY,
bin VARCHAR2(20) PATH '$.bin',
hits VARCHAR2(20) PATH '$.hits',
first_date VARCHAR2(8) PATH '$.first_date'
)
) j
WHERE rn = 1
输出:
BIN | HITS | FIRST_DATE :--- | :--- | :------------------ 1290 | 1 | 2015-11-03 00:00:00
db<>fiddle here