【问题标题】:Get value from json dimensional array in oracle从oracle中的json维度数组中获取值
【发布时间】:2020-10-28 09:33:58
【问题描述】:

我有以下 JSON,我需要从中获取 issuedIdentValue 的值,其中 issuedIdentType = PANCARD

{
    "issuedIdent": [ 
        {"issuedIdentType":"DriversLicense","issuedIdentValue":"9797979797979797"},
        {"issuedIdentType":"SclSctyNb","issuedIdentValue":"078-01-8877"},
        {"issuedIdentType":"PANCARD","issuedIdentValue":"078-01-8877"}
    ]
}

我无法在下面的查询中对索引值 [2] 进行硬编码,因为这些记录的顺序可以更改。所以想摆脱任何硬编码的索引。

select json_value(
    '{"issuedIdent": [{"issuedIdentType":"DriversLicense","issuedIdentValue":"9797979797979797"},{"issuedIdentType":"SclSctyNb","issuedIdentValue":"078-01-8877"},  {"issuedIdentType":"PANCARDSctyNb","issuedIdentValue":"078-01-8877"}]}', 
    '$.issuedIdent[2].issuedIdentValue'
) as output 
from d1entzendev.ExternalEventLog 
where 
    eventname = 'CustomerDetailsInqSVC' 
    and applname = 'digitalBANKING' 
    and requid = '4fe1fa1b-abd4-47cf-834b-858332c31618';

需要对 json_value 函数进行哪些更改才能达到预期结果

【问题讨论】:

    标签: sql arrays json oracle where-clause


    【解决方案1】:

    在 Oracle 12c 或更高版本中,您可以为此使用 JSON_TABLE()

    select value
    from json_table(
        '{"issuedIdent": [{"issuedIdentType":"DriversLicense","issuedIdentValue":"9797979797979797"},{"issuedIdentType":"SclSctyNb","issuedIdentValue":"078-01-8877"},  {"issuedIdentType":"PANCARD","issuedIdentValue":"078-01-8877"}]}',
        '$.issuedIdent[*]' columns
             type   varchar(50) path '$.issuedIdentType',
             value  varchar(50) path '$.issuedIdentValue'
    ) t
    where type = 'PANCARD'
    

    This returns

    |价值 | | :------------ | | 078-01-8877 |

    【讨论】:

    • 如果我必须从同一个 JSON 中获取 5 个不同的数组集,那么我将不得不运行 5 个 SQL 语句。是否有任何其他解决方案可以让我从 json_value 或任何其他单个函数获得预期结果...只是想避免多个选择语句
    • 对我之前的评论有什么建议吗?
    • @deltaforce:没有看到实际数据,我真的无法判断。我建议您为此提出一个新问题,提供适当的样本数据和所需的结果。
    猜你喜欢
    • 2018-11-21
    • 2022-07-07
    • 2018-06-23
    • 2019-04-02
    • 2019-03-04
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多