当前位置:网站首页>Mysql5.6 parsing JSON strings (supporting complex nested formats)
Mysql5.6 parsing JSON strings (supporting complex nested formats)
2022-07-05 17:21:00 【1024 questions】
mysql5.6 analysis JSON character string
Support complex nested formats
mysql5.6 And the following analysis json Method
Let's talk about the background of the problem first
Here is the corresponding code
mysql5.6 analysis JSON character string Support complex nested formatsI don't say much nonsense , On the first code .
CREATE FUNCTION `json_parse`(`jsondata` longtext,`keyname` text) RETURNS text CHARSET utf8BEGINDECLARE delim VARCHAR(128);DECLAREresult longtext;DECLARE startpos INTEGER;DECLARE endpos INTEGER;DECLARE endpos1 INTEGER; DECLARE findpos INTEGER;DECLARE leftbrace INTEGER;DECLARE tmp longtext; DECLARE tmp2 longtext;DECLARE Flag INTEGER;SET delim = CONCAT('"', keyname, '": "');SET startpos = locate(delim,jsondata);IF startpos > 0 THENSET findpos = startpos+length(delim);SET leftbrace = 1;SET endpos = 0;SET Flag =1;get_token_loop: repeat IF substr(jsondata,findpos,2)='\\"' THENSET findpos = findpos + 2;iterate get_token_loop;ELSEIF substr(jsondata,findpos,2)='\\\\' THENSET findpos = findpos + 2;iterate get_token_loop;ELSEIF substr(jsondata,findpos,1)='"' AND Flag = 1 THEN SET endpos = findpos;SET findpos = LENGTH(jsondata)+1;leave get_token_loop;END IF;SET findpos = findpos + 1;UNTIL findpos > LENGTH(jsondata) END repeat;IF endpos > 0 THENSELECT substr(jsondata,startpos+length(delim)# Take out value The starting position of the value ,endpos# Take out value The end position of the value -(startpos+length(delim))# subtract value The starting position of the value , obtain value Value character length ) INTO resultFROM DUAL;SET result= replace(result,'\\"','"');SET result= replace(result,'\\\\','\\');ELSE SET result=null;END IF;/*SELECT substr(jsondata,locate(delim,jsondata)+length(delim)# Take out value The starting position of the value ,locate('"',jsondata,locate(delim,jsondata)+length(delim))# Take out value The end position of the value -(locate(delim,jsondata)+length(delim))# subtract value The starting position of the value , obtain value Value character length ) INTO resultFROM DUAL;*/ELSESET delim = CONCAT('"', keyname, '": {');SET startpos = locate(delim,jsondata);IF startpos > 0 THENSET findpos = startpos+length(delim);SET leftbrace = 0;SET endpos = 0;SET Flag =0;get_token_loop: repeat IF substr(jsondata,findpos,2)='{"' THENSET leftbrace = leftbrace + 1;SET findpos = findpos + 2;iterate get_token_loop;ELSEIF substr(jsondata,findpos,2)='\\"' THENSET findpos = findpos + 2;iterate get_token_loop;ELSEIF substr(jsondata,findpos,3)=': "' THENSET Flag = 1;SET findpos = findpos + 3;iterate get_token_loop;ELSEIF substr(jsondata,findpos,1)='"' THENSET Flag = 0;ELSEIF substr(jsondata,findpos,1)='}' AND Flag = 0 THENIF leftbrace > 0 THENSET leftbrace = leftbrace - 1;ELSE SET endpos = findpos;SET findpos = LENGTH(jsondata)+1;END IF;END IF;SET findpos = findpos + 1;UNTIL findpos > LENGTH(jsondata) END repeat;IF endpos > 0 THENSELECT substr(jsondata,startpos+length(delim)# Take out value The starting position of the value ,endpos# Take out value The end position of the value -(startpos+length(delim))# subtract value The starting position of the value , obtain value Value character length ) INTO resultFROM DUAL;SET result=CONCAT("{",result, '}');ELSE SET result=null;END IF;ELSE SET delim = CONCAT('"', keyname, '": [');SET startpos = locate(delim,jsondata);IF startpos > 0 THENSET findpos = startpos+length(delim);SET leftbrace = 0;SET endpos = 0;SET tmp = substring_index(jsondata,delim,-1);SET tmp2 = substring_index(tmp,']',1); IF locate('[',tmp2) =0 THENSET endpos = locate(']',tmp);SET endpos = endpos+findpos-1; ELSEget_token_loop: repeat IF substr(jsondata,findpos,2)='\\"' THENSET findpos = findpos + 2;iterate get_token_loop;ELSEIF substr(jsondata,findpos,3)=': "' THENSET Flag = 1;SET findpos = findpos + 3;iterate get_token_loop;ELSEIF substr(jsondata,findpos,1)='[' AND Flag = 0 THENSET leftbrace = leftbrace + 1;SET findpos = findpos + 1;iterate get_token_loop;ELSEIF substr(jsondata,findpos,1)='"' THENSET Flag = 0;ELSEIF substr(jsondata,findpos,1)=']' AND Flag = 0 THENIF leftbrace > 0 THENSET leftbrace = leftbrace - 1;ELSE SET endpos = findpos;SET findpos = LENGTH(jsondata)+1;END IF;END IF;SET findpos = findpos + 1;UNTIL findpos > LENGTH(jsondata) END repeat;END IF;IF endpos > 0 THENSELECT substr(jsondata,startpos+length(delim)# Take out value The starting position of the value ,endpos# Take out value The end position of the value -(locate(delim,jsondata)+length(delim))# subtract value The starting position of the value , obtain value Value character length ) INTO resultFROM DUAL;SET result=CONCAT("[",result, ']');ELSE SET result=null;END IF;ELSE SET delim = CONCAT('"', keyname, '": ');SET startpos = locate(delim,jsondata);IF startpos > 0 THENSET endpos = locate(',',jsondata,startpos+length(delim));SET endpos1 = locate('}',jsondata,startpos+length(delim));IF endpos>0 OR endpos1>0 THENIF endpos1>0 AND endpos1 < endpos OR endpos =0 THENSET endpos = endpos1;END IF;SELECT substr(jsondata,startpos+length(delim)# Take out value The starting position of the value ,endpos# Take out value The end position of the value -(locate(delim,jsondata)+length(delim))# subtract value The starting position of the value , obtain value Value character length ) INTO resultFROM DUAL;IF STRCMP(result,'null')=0 THENSET result=null;END IF;ELSE SET result=null;END IF;ELSE SET result=null;END IF;END IF;END IF;END IF;if result='' and RIGHT(keyname,2)='Id' thenSET result=null;end if;RETURN result;END
jsondata It needs to be strict json Format ( Notice the space between commas and semicolons and double quotes )
SET jsondata='{"CurrentPage": 1, "data": [{"config": "123"}, {"config": "456"}], "PageSize": 10}' SELECT json_parse(jsondata, 'CurrentPage') INTO CurrentPage;SELECT json_parse(jsondata, 'data') INTO data;
If you want to get config The content of , You can do this
SET count = (LENGTH(data)-LENGTH(REPLACE(data,'},','')))/2+1; SET i = 0; WHILE i < count DO SET SetObject = SUBSTRING_INDEX(SUBSTRING_INDEX(data,'},',i+1),'},',-1); IF LENGTH(SetObject)>0 THEN SELECT json_parse(SetObject, 'config') INTO config; END IF; SET i = i + 1; END WHILE;
deficiencies ,jsondata When there is a lot of data , There will be efficiency problems .
mysql5.6 And the following analysis json MethodPreviously, I found that the online query platform in the company is MySQL5.6, Out-of-service JSON_EXTRACT, Nor can you use stored procedures , So I can only compile a simple query by myself , A few pieces of data can still be checked , If there's a lot of data , It is estimated that more resources will be consumed .
Let's talk about the background of the problem firstI want to '{"platform":"Android","source":"tt","details":null}' Found in this string of things source This key Corresponding value value .
This method is to find source":" The starting position and length of this string , In this way, we can find value The starting position of the value ; The first one after finding this string " Position of appearance , You can get value The end position of the value .
recycling substr function , You can take out the corresponding position .
Here is the corresponding codeSELECT '{"platform":"Android","source":"tt","details":null}' as 'sample',substr( '{"platform":"Android","source":"tt","details":null}' ,locate('source":"','{"platform":"Android","source":"tt","details":null}') +length('source":"')# Take out value The starting position of the value ,locate( '"' ,'{"platform":"Android","source":"tt","details":null}' ,locate('source":"','{"platform":"Android","source":"tt","details":null}') +length('source":"') )# Take out value The end position of the value -( locate('source":"','{"platform":"Android","source":"tt","details":null}') +length('source":"') )# subtract value The starting position of the value , obtain value Value character length ) as resultFROM DUAL
After running , You get result Result , Namely tt. If you need other elements , Just replace the corresponding key Values and fields , Just fine .
The above is personal experience , I hope I can give you a reference , I also hope you can support the software development network .
边栏推荐
猜你喜欢
winedt常用快捷键 修改快捷键latex编译按钮
Embedded -arm (bare board development) -1
High number | summary of calculation methods of volume of rotating body, double integral calculation of volume of rotating body
拷贝方式之DMA
激动人心!2022开放原子全球开源峰会报名火热开启!
thinkphp3.2.3
Iphone14 with pill screen may trigger a rush for Chinese consumers
Judge whether a number is a prime number (prime number)
项目引入jar从私服Nexus 拉去遇到的一个问题
精准防疫有“利器”| 芯讯通助力数字哨兵护航复市
随机推荐
How does the outer disk futures platform distinguish formal security?
Read the basic grammar of C language in one article
编译libssh2报错找不到openssl
The survey shows that the failure rate of traditional data security tools in the face of blackmail software attacks is as high as 60%
Embedded-c Language-2
【Web攻防】WAF检测技术图谱
composer安装报错:No composer.lock file present.
CMake教程Step6(添加自定义命令和生成文件)
VBA驱动SAP GUI实现办公自动化(二):判断元素是否存在
IDC报告:腾讯云数据库稳居关系型数据库市场TOP 2!
Debug kernel code through proc interface
The first EMQ in China joined Amazon cloud technology's "startup acceleration - global partner network program"
Embedded-c language-6
easyNmon使用汇总
CMake教程Step1(基本起点)
How can C TCP set heartbeat packets to be elegant?
Wechat official account web page authorization login is so simple
thinkphp模板的使用
张平安:加快云上数字创新,共建产业智慧生态
【7.7直播预告】《SaaS云原生应用典型架构》大咖讲师教你轻松构建云原生SaaS化应用,难题一一击破,更有华为周边好礼等你领!