【问题标题】:Oracle extract json fields with oracle regexp_substrOracle 使用 oracle regexp_substr 提取 json 字段
【发布时间】:2021-04-23 11:10:51
【问题描述】:

我正在使用带有 regexp_substr 的 Oracle 查询从 JSON 字符串中提取 json 字段。我想要“pickupLocation”的编号(1177):“1177”,但我使用的这个查询不起作用

select  to_char(regexp_substr(a.input_msg, '(\w*)("pickupLocation":")(\w*)(")',1,1)) 
from td_interface_phxsale_log a

我的查询输出:"pickupLocation":"1177"

myjson 数据:

{
  "shiptoAddr": "",
  "shippingCostAmt": "",
  "pickupLocation": "1177"
}

【问题讨论】:

    标签: sql json oracle


    【解决方案1】:

    一般来说,您不应尝试使用正则表达式解析 JSON 内容。假设你真的需要走这条路,你可以试试:

    SELECT regexp_substr(input_msg, '"pickupLocation": "([^"]+)"', 1, 1, NULL, 1)
    FROM td_interface_phxsale_log;
    

    Demo

    注意:由于 JSON 已经是文本,因此无需转换使用 TO_CHAR 提取的内容。

    【讨论】:

    • 非常感谢。注意:由于 JSON 已经是文本,因此无需转换使用 TO_CHAR 提取的内容。 >>> 我的字段类型是 clob
    【解决方案2】:

    为什么要使用正则表达式?这完全没有意义。你为你的 Oracle 许可证支付了很多钱,使用它附带的 JSON 工具。

    with
      inputs (my_json_input) as (
        select to_clob('{
                          "shiptoAddr": "",
                          "shippingCostAmt": "",
                          "pickupLocation": "1177"
                        }')
        from   dual
      )
    -- End of simulated inputs (for testing only; 
    -- remove the WITH clause and use your actual table and column names below)
    --
    select json_value(my_json_input, '$.pickupLocation') as pickuplocation
    from   inputs
    ;
    
    PICKUPLOCATION 
    ---------------
    1177
    

    这里不需要to_char();即使 JSON 字符串是 CLOB,json_value() 也会返回 varchar2(除非使用 returning 子句明确要求)。

    如果实际上“拾取位置”数据类型是 number(显然应该如此),您可以将 returning number 粘贴在 json_value() 调用的末尾(在右括号之前)。

    【讨论】:

      猜你喜欢
      • 2016-07-28
      • 2012-06-26
      • 2020-10-10
      • 2021-09-16
      • 1970-01-01
      • 2014-08-18
      • 2021-12-18
      • 2021-08-04
      • 2016-05-22
      相关资源
      最近更新 更多