当前位置:网站首页>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 .
边栏推荐
- 项目引入jar从私服Nexus 拉去遇到的一个问题
- 基于Redis实现延时队列的优化方案小结
- CMake教程Step6(添加自定义命令和生成文件)
- 33: Chapter 3: develop pass service: 16: use redis to cache user information; (to reduce the pressure on the database)
- CMake教程Step5(添加系统自检)
- mysql如何使用JSON_EXTRACT()取json值
- Iphone14 with pill screen may trigger a rush for Chinese consumers
- High number | summary of calculation methods of volume of rotating body, double integral calculation of volume of rotating body
- mysql中取出json字段的小技巧
- thinkphp3.2.3
猜你喜欢
CMake教程Step2(添加库)
The two ways of domestic chip industry chain go hand in hand. ASML really panicked and increased cooperation on a large scale
URP下Alpha从Gamma空间到Linner空间转换(二)——多Alpha贴图叠加
Etcd 构建高可用Etcd集群
[first lecture on robot coordinate system]
stirring! 2022 open atom global open source summit registration is hot!
激动人心!2022开放原子全球开源峰会报名火热开启!
拷贝方式之DMA
thinkphp模板的使用
Tips for extracting JSON fields from MySQL
随机推荐
Tips for extracting JSON fields from MySQL
Is it safe to open a securities account by mobile phone? Detailed steps of how to buy stocks
激动人心!2022开放原子全球开源峰会报名火热开启!
[Web attack and Defense] WAF detection technology map
The two ways of domestic chip industry chain go hand in hand. ASML really panicked and increased cooperation on a large scale
The survey shows that the failure rate of traditional data security tools in the face of blackmail software attacks is as high as 60%
ternary operator
网上办理期货开户安全吗?网上会不会骗子比较多?感觉不太靠谱?
Embedded -arm (bare board development) -1
thinkphp3.2.3
Practical example of propeller easydl: automatic scratch recognition of industrial parts
mysql5.6解析JSON字符串方式(支持复杂的嵌套格式)
dried food! Semi supervised pre training dialogue model space
基于51单片机的电子时钟设计
How to write a full score project document | acquisition technology
Embedded-c Language-2
Error in composer installation: no composer lock file present.
ThoughtWorks global CTO: build the architecture according to needs, and excessive engineering will only "waste people and money"
C#(Winform) 当前线程不在单线程单元中,因此无法实例化 ActiveX 控件
【Web攻防】WAF检测技术图谱