当前位置:网站首页>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 .
边栏推荐
- Use byte stream to read Chinese from file to console display
- 【剑指 Offer】63. 股票的最大利润
- Application of threshold homomorphic encryption in privacy Computing: Interpretation
- 高数 | 旋转体体积计算方法汇总、二重积分计算旋转体体积
- MySql 查询符合条件的最新数据行
- 机器学习01:绪论
- [Jianzhi offer] 63 Maximum profit of stock
- [wechat applet] read the life cycle and route jump of the applet
- Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
- mysql5.6解析JSON字符串方式(支持复杂的嵌套格式)
猜你喜欢

Use of ThinkPHP template

浏览器渲染原理以及重排与重绘
SQL删除重复数据的实例教程

33: Chapter 3: develop pass service: 16: use redis to cache user information; (to reduce the pressure on the database)

American chips are no longer proud, and Chinese chips have successfully won the first place in emerging fields
mysql中取出json字段的小技巧

ECU简介

WR | Jufeng group of West Lake University revealed the impact of microplastics pollution on the flora and denitrification function of constructed wetlands

干货!半监督预训练对话模型 SPACE

Use JDBC technology and MySQL database management system to realize the function of course management, including adding, modifying, querying and deleting course information.
随机推荐
深耕5G,芯讯通持续推动5G应用百花齐放
MySql 查询符合条件的最新数据行
Example tutorial of SQL deduplication
手机开证券账户安全吗?怎么买股票详细步骤
什么是ROM
基于Redis实现延时队列的优化方案小结
Embedded UC (UNIX System Advanced Programming) -2
机器学习01:绪论
CMake教程Step2(添加库)
mysql5.6解析JSON字符串方式(支持复杂的嵌套格式)
【剑指 Offer】63. 股票的最大利润
Embedded-c Language-3
Is it safe and reliable to open futures accounts on koufu.com? How to distinguish whether the platform is safe?
What else do you not know about new map()
【7.7直播预告】《SaaS云原生应用典型架构》大咖讲师教你轻松构建云原生SaaS化应用,难题一一击破,更有华为周边好礼等你领!
Etcd build a highly available etcd cluster
【机器人坐标系第一讲】
调查显示传统数据安全工具面对勒索软件攻击的失败率高达 60%
浏览器渲染原理以及重排与重绘
composer安装报错:No composer.lock file present.