当前位置:网站首页>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;ENDjsondata 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 .
边栏推荐
猜你喜欢

Machine learning compilation lesson 2: tensor program abstraction

composer安装报错:No composer.lock file present.

NPM installation

dried food! Semi supervised pre training dialogue model space
![[Web attack and Defense] WAF detection technology map](/img/7c/60a25764950668ae454b2bc08fe57e.png)
[Web attack and Defense] WAF detection technology map

项目引入jar从私服Nexus 拉去遇到的一个问题

阈值同态加密在隐私计算中的应用:解读
What are the precautions for MySQL group by

thinkphp3.2.3

Rider 设置选中单词侧边高亮,去除警告建议高亮
随机推荐
mysql5.6解析JSON字符串方式(支持复杂的嵌套格式)
NPM installation
[wechat applet] read the life cycle and route jump of the applet
Precision epidemic prevention has a "sharp weapon" | smart core helps digital sentinels escort the resumption of the city
C#(Winform) 当前线程不在单线程单元中,因此无法实例化 ActiveX 控件
Machine learning 02: model evaluation
The second day of learning C language for Asian people
ternary operator
It is forbidden to copy content JS code on the website page
Excuse me, is the redis syntax used in DMS based on the commands of the redis community version of the cloud database
浏览器渲染原理以及重排与重绘
張平安:加快雲上數字創新,共建產業智慧生態
深耕5G,芯讯通持续推动5G应用百花齐放
Embedded UC (UNIX System Advanced Programming) -2
Rider 设置选中单词侧边高亮,去除警告建议高亮
Tips for extracting JSON fields from MySQL
Machine learning compilation lesson 2: tensor program abstraction
齐宣王典故
项目引入jar从私服Nexus 拉去遇到的一个问题
MySQL queries the latest qualified data rows