oracle 在oracle中解析JSON字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25804193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:30:35  来源:igfitidea点击:

parsing JSON string in oracle

jsonoracle

提问by Kishore Kumar

i have JSON string in one column in oracle 10g database like

我在 oracle 10g 数据库中的一列中有 JSON 字符串,例如

[{"id":"1","contactBy":"Rajesh Kumar"},{"id":"2","contactBy":"Rakesh Kumar"}]

I have to get the value for ContactByin that column for one of the reports.

对于其中一份报告,我必须在该列中获取ContactBy的值。

is there any built in function to parse the JSON string in Oracle 10g or any user defined funciton to parse the String

是否有任何内置函数来解析 Oracle 10g 中的 JSON 字符串或任何用户定义的函数来解析字符串

回答by Emmanuel

As said by Jens in comments, JSON support is only available from 12c, but you can use regular expressions as a workaround to get what you want:

正如 Jens 在评论中所说,JSON 支持仅在 12c 中可用,但您可以使用正则表达式作为解决方法来获得您想要的:

select regexp_replace(regexp_substr('[{"id": "1", "contactBy":"Rajesh Kumar"},{"id": "2","contactBy": "Emmanuel Test"}]',
                                    '"contactBy":\s*("(\w| )*")', 1, level),
                     '"contactBy":\s*"((\w| )*)"', '', 1, 1) contact
from dual
connect by regexp_substr('[{"id": "1","contactBy":"Rajesh Kumar"},{"id": "2","contactBy": "Emmanuel Test"}]', '"contactBy":\s*("(\w| )*")', 1, level) is not null
;

EDIT: request modified to take both special characters and display answers in a single row:

编辑:请求修改为采用特殊字符并在一行中显示答案:

select listagg(contact, ', ') within group (order by lev)
from
(
  select regexp_replace(regexp_substr('[{"id": "1", "contactBy":"Rajesh Kumar"},{"id": "2","contactBy": "Emmanuel Test+-"}]',
                                      '"contactBy":\s*(".*?")', 1, level),
                       '"contactBy":\s*"(.*?)"', '', 1, 1) contact, level lev
  from dual
  connect by regexp_substr('[{"id": "1","contactBy":"Rajesh Kumar"},{"id": "2","contactBy": "Emmanuel Test+-"}]', '"contactBy":\s*(".*?")', 1, level) is not null
)
;

回答by Kishore Kumar

@ Emmanuel your code is really helped a lot, thank you very much. but your query is taking too much of time, so i changed to a function , which will return the required values.

@Emmanuel 你的代码真的很有帮助,非常感谢。但是您的查询花费了太多时间,所以我更改为一个函数,它将返回所需的值。

CREATE OR REPLACE FUNCTION SFGETCRCONTACTBY(INCRID NUMBER) RETURN VARCHAR2 AS
TEMPINT NUMBER :=0;
OUTPUT VARCHAR2(10000) ;
TEMPVAR VARCHAR2(1000);

BEGIN

SELECT   REGEXP_COUNT(CR_CONTACT_BY, '"contactBy":\S*(".*?")')
INTO TEMPINT 
FROM T_LOAN_REQUEST_MARKET WHERE CR_ID=INCRID;
WHILE TEMPINT > 0
LOOP
SELECT REGEXP_REPLACE(REGEXP_SUBSTR(CR_CONTACT_BY, '"contactBy":\S*(".*?")', 1,TEMPINT),  '"contactBy":\S*"(.*?)"', '', 1, 1) INTO TEMPVAR
FROM T_LOAN_REQUEST_MARKET WHERE CR_ID=INCRID;
IF OUTPUT IS  NULL  THEN
    OUTPUT :=   TEMPVAR;
 ELSE 
    OUTPUT :=  OUTPUT ||',' || TEMPVAR;
END IF;

TEMPINT := TEMPINT-1;
END LOOP;

RETURN OUTPUT;
END;
/