mysql 5 7 5 6 以下解析json字串

2021-09-25 23:52:28 字數 1372 閱讀 4794

因專案需要編寫包含解析json字串語句的mysql函式,只能將5.6公升級到了5.7,本以為就此完事,但誰知後來業務需要用到中文字串key型別的json,這下5.7就歇菜了,因為原生函式不支援中文字串作為key的解析,網上找了很多網友自定**析json的函式,發現支援度都有不少問題,於是自己寫了個最土的實現(如下),有需要的朋友拿去用吧

delimiter $$

drop function if exists `json_extract_c`$$

create function `json_extract_c`(

details text,

required_field varchar (255)

) returns text charset utf8

begin

declare scount int(3) default 0;

set scount = length(details)-length(replace(details,'

set scount = length(details)-length(replace(details,'}',''));

set details = substring_index(details, "}", scount); -- "a":"a","b":"b","c":"c"

set required_field = concat('"',substring_index(required_field,'$.', -1),'":');

set details = trim(substring_index(details, required_field, -1)); -- "b","c":"c"

if locate('

return details;

elseif locate('"', details) <> 1 then -- b,"c":"c"或b

if locate(',', details) > 1 then

set details = substring_index(details, ',', 1);

end if;

return trim(details);

end if;

set scount = length(details)-length(replace(details,'"',''));

set details = substring_index(details, '"', 0 - scount); -- :"b","c":"c"}

set details = substring_index(details, '"', 1); -- b

return details;

end$$

delimiter ;

mysql解析json字串

1.對於mysql5.7以上版本 使用mysql的內建函式json extract column,key 這個函式有兩個引數,第乙個引數column代表json列的列名 第二個引數key代表json字串中的某乙個key。select json extract pricediscount as 定價折...

mysql5 7以下自定義函式解析json

delimiter use dw drop function if exists fn json getkeyvalue create definer data function fn json getkeyvalue in jsonarray varchar 4096 json陣列字串 in in...

Mysql解析json字串 陣列

1 mysql解析json字串 解決方法 json extract 原欄位,json欄位名 執行sql select json extract t.result,row json extract t.result,value json extract t.result,criteria from t...