当前位置:网站首页>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 .
边栏推荐
猜你喜欢
mysql中取出json字段的小技巧
![[Web attack and Defense] WAF detection technology map](/img/7c/60a25764950668ae454b2bc08fe57e.png)
[Web attack and Defense] WAF detection technology map

The survey shows that the failure rate of traditional data security tools in the face of blackmail software attacks is as high as 60%
一文了解MySQL事务隔离级别

China Radio and television officially launched 5g services, and China Mobile quickly launched free services to retain users

机器学习02:模型评估

CMake教程Step1(基本起点)

Copy mode DMA

7.Scala类

thinkphp3.2.3
随机推荐
菜刀,蚁剑,冰蝎,哥斯拉的流量特征
Etcd 构建高可用Etcd集群
ClickHouse(03)ClickHouse怎么安装和部署
调查显示传统数据安全工具面对勒索软件攻击的失败率高达 60%
Embedded -arm (bare board development) -2
Timestamp strtotime the day before or after the date
Matery主题自定义(一)黑夜模式
【性能测试】jmeter+Grafana+influxdb部署实战
Copy mode DMA
The second day of learning C language for Asian people
VBA驱动SAP GUI实现办公自动化(二):判断元素是否存在
easyNmon使用汇总
It is forbidden to copy content JS code on the website page
Embedded-c Language-4
How does the outer disk futures platform distinguish formal security?
【剑指 Offer】66. 构建乘积数组
Thoughtworks 全球CTO:按需求构建架构,过度工程只会“劳民伤财”
Writing method of twig array merging
ECU introduction
Is it safe to open futures accounts online? Will there be more liars online? Doesn't feel very reliable?