当前位置:   article > 正文

Oracle 获取json字符串指定内容_oracle sql取json中的字段值

oracle sql取json中的字段值

1.创建FN_SPLIT切割函数

  1. CREATE OR REPLACE TYPE ty_row_str_split as object (strValue VARCHAR2 (4000));
  2. CREATE OR REPLACE TYPE ty_tbl_str_split IS TABLE OF ty_row_str_split;
  3. CREATE OR REPLACE FUNCTION FN_SPLIT ( p_str IN CLOB, p_delimiter IN VARCHAR2 ) RETURN ty_tbl_str_split IS j INT := 0;
  4. i INT := 1;
  5. len INT := 0;
  6. len1 INT := 0;
  7. str VARCHAR2 ( 5000 );
  8. str_split ty_tbl_str_split := ty_tbl_str_split ( );
  9. BEGIN
  10. len := LENGTH( p_str );
  11. len1 := LENGTH( p_delimiter );
  12. WHILE
  13. j < len
  14. LOOP
  15. j := INSTR( p_str, p_delimiter, i );
  16. IF
  17. j = 0 THEN
  18. j := len;
  19. str := SUBSTR( p_str, i );
  20. str_split.EXTEND;
  21. str_split ( str_split.COUNT ) := ty_row_str_split ( strValue => str );
  22. IF
  23. i >= len THEN
  24. EXIT;
  25. END IF;
  26. ELSE str := SUBSTR( p_str, i, j - i );
  27. i := j + len1;
  28. str_split.EXTEND;
  29. str_split ( str_split.COUNT ) := ty_row_str_split ( strValue => str );
  30. END IF;
  31. END LOOP;
  32. RETURN str_split;
  33. END FN_SPLIT;

2.创建 JSON_KEY()函数

  1. CREATE OR REPLACE function "JSON_KEY"(p_jsonstr CLOB,p_key varchar2)
  2. return varchar2
  3. is
  4. rtnval varchar2(1000);
  5. i number(2);
  6. jsonkey varchar2(5000);
  7. jsonvalue varchar2(3000);
  8. json CLOB;
  9. begin
  10. if p_jsonstr is not null then
  11. json := replace(p_jsonstr,'{','') ;
  12. json := replace(json,'}','') ;
  13. json := replace(json,'"','') ;
  14. for temprow in(select strvalue as value from table(fn_split(json, ','))) loop
  15. if temprow.value is not null then
  16. i := 0;
  17. jsonkey :='';
  18. jsonvalue := '';
  19. for tem2 in(select strvalue as value from table(fn_split(temprow.value, ':'))) loop
  20. if i = 0 then
  21. jsonkey := tem2.value;
  22. end if;
  23. if i = 1 then
  24. jsonvalue := tem2.value;
  25. end if;
  26. i := i + 1;
  27. end loop;
  28. if(jsonkey = p_key) then
  29. rtnval := jsonvalue;
  30. end if;
  31. end if;
  32. end loop;
  33. end if;
  34. return rtnval;
  35. end JSON_KEY;

用法如下:
select JSON_KEY('{"name":"zhangsan","tel":"138888888"}','tel') from dual

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/977819
推荐阅读
相关标签
  

闽ICP备14008679号