JSON Oracle SQL 解析/取消嵌套嵌入式 JSON 转义形式的数据

JSON Oracle SQL parsing / unnest embedded JSON data in escaped form

这是我的 JSON 存储在 CLOB 列中:

select upJSON from myLocations;

{"values":[
{"nameValuePairs":{"upJSON":"{\"mResults\":[0.0,0.0],\"mProvider\":\"fused\",\"mDistance\":0.0,\"mAltitude\":0.0}","id":"1","updated":"2015-03-30 20:28:51"}},
{"nameValuePairs":{"upJSON":"{\"mResults\":[0.0,0.0],\"mProvider\":\"FINDME\",\"mDistance\":0.0,\"mAltitude\":22.2}","id":"2","updated":"2015-03-30 20:28:53"}},
{"nameValuePairs":{"upJSON":"{\"mResults\":[0.0,0.0],\"mProvider\":\"fused\",\"mDistance\":0.0,\"mAltitude\":0.0}","id":"3","updated":"2015-03-30 20:28:55"}},
{"nameValuePairs":{"upJSON":"{\"mResults\":[0.0,0.0],\"mProvider\":\"fused\",\"mDistance\":0.0,\"mAltitude\":0.0}","id":"4","updated":"2015-03-30 20:28:57"}}
]}           

(为清楚起见,我插入了换行符)

请:select 需要什么 SQL(或 PL/SQL)只是 mProvider 的值、mAltitude 和来自第二个 "nameValuePairs" 的 id (= "FINDME" and 22.2 and "2") 在上面的例子中) ??

由于您使用的是 12c,因此您可以访问本机 JSON 解析(只要您的 CLOB 列具有 is json 检查约束)。

一些好的背景资料可在以下网址获得:

https://docs.oracle.com/database/121/ADXDB/json.htm#ADXDB6371

如果您 JSON 看起来像这样:

{
    "values": [
        {
            "nameValuePairs": {
                "upJSON": {
                    "mResults": [
                        "0.0",
                        "0.0"
                    ],
                    "mProvider": "fused",
                    "mDistance": "0.0",
                    "mAltitude": "22.2"
                },
                "id": "1",
                "updated": "2015-03-30 20:28:51"
            }
        },
        ...
        ...

虽然,当我将问题中的片段放入 JSONLint 时 returns:

{
    "values": [
        {
            "nameValuePairs": {
                "upJSON": "{\"mResults\":[0.0,0.0],\"mProvider\":\"fused\",\"mDistance\":0.0,\"mAltitude\":0.0}",
                "id": "1",
                "updated": "2015-03-30 20:28:51"
            }
        },
        {
            "nameValuePairs": {
                "upJSON": "{\"mResults\":[0.0,0.0],\"mProvider\":\"FINDME\",\"mDistance\":0.0,\"mAltitude\":22.2}",
                "id": "2",
                "updated": "2015-03-30 20:28:53"
            }
        },

类似下面的内容可能会让您入门:

select
    upJSON.values
from
    myLocations
where
    json_value(upJSON, '$nameValuePairs.id' returning varchar2 error on error) = '2';

如果要将查询限制为单个 ID,则需要将全文或基于函数的索引添加到 JSON 列。

https://odieweblog.wordpress.com/2015/04/12/json_table-chaining/#comment-1025

with tmp as (
    SELECT /*+ no_merge */ d.*
    FROM ulocations ul,
         json_table(ul.upjson, '$'
         columns(
         NESTED PATH '$.values[*].nameValuePairs'
               COLUMNS (
                   updated VARCHAR2(19 CHAR)  PATH '$.updated'
                 , id varchar2(9 char)        path '$._id'
                 , upJSON VARCHAR2(2000 CHAR) PATH '$.upJSON'
                )) ) d
    --where d.id = '0'
    )
    select t.updated
        , t.id
        , jt2.*
    from tmp t
       , json_table(t.upJSON, '$'
          columns mProvider varchar2(5) path '$.mProvider'
                , mLongitude  number    path '$.mLongitude'
        ) jt2
    ;