MySQL (5.6 and below) parses json #json parsing function DELIMITER $$ DROP FUNCTION IF EXISTS `json_extract_c`$$ CREATE FUNCTION `json_extract_c`( details TEXT, required_field VARCHAR (255) ) RETURNS TEXT CHARSET latin1 BEGIN SET details = SUBSTRING_INDEX(details, "{", -1); SET details = SUBSTRING_INDEX(details, "}", 1); RETURN TRIM( BOTH '"' FROM SUBSTRING_INDEX( SUBSTRING_INDEX( SUBSTRING_INDEX( details, CONCAT( '"', SUBSTRING_INDEX(required_field,'$.', -1), '":' ), -1 ), ',"', 1 ), ':', -1 ) ) ; END$$ DELIMITER ; example: select json_extract_c(json, "$.totalDays"), json from item limit 100; Self-Test CREATE TABLE `json_test` ( `id` int(11) DEFAULT NULL, `person_desc` text COLLATE utf8mb4_unicode_ci ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; insert INTO json_test values(2,'{"firstName":"Sergei","lastName":"Rachmaninoff","instrument":"piano"}'); SELECT id,json_extract_c(person_desc,'$.lastName') as "keys" FROM json_test; Improved: if the key is not found, return ''. Previously, if the key is not found, the first value is found. CREATE DEFINER=`zhangfen`@`%` FUNCTION `json_extract_c`( details TEXT, required_field VARCHAR (255) ) RETURNS text CHARSET latin1 BEGIN SET details = SUBSTRING_INDEX(details, "{", -1); SET details = SUBSTRING_INDEX(details, "}", 1); RETURN TRIM( BOTH '"' FROM SUBSTRING_INDEX( SUBSTRING_INDEX( SUBSTRING_INDEX( CONCAT('"":"",',details), CONCAT( '"', SUBSTRING_INDEX(required_field,'$.', -1), '":' ), -1 ), ',"', 1 ), ':', -1 ) ) ; END Summarize The above is a detailed example of how to parse JSON in MySQL (5.6 and below) introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website! You may also be interested in:
|
<<: VMwarea virtual machine installation win7 operating system tutorial diagram
>>: Installation and configuration method of vue-route routing management
Table of contents Two ways to solve the problem o...
Long story short, today we will talk about using ...
Table of contents Partitioning mechanism SELECT q...
Table of contents Preface: Detailed introduction:...
This article shares the encapsulation code of Jav...
When changing the time zone under Linux, it is al...
I found an example when I was looking for a way t...
1. Request answer interface 2. Determine whether ...
The default operating mode of MySQL is autocommit...
Compared with fdisk, parted is less used and is m...
This article example shares the specific code of ...
Problem [root@zh ~]# [root@zh ~]# [root@zh ~]# yu...
MySQL 5.7.20 installation and configuration metho...
The TextBox with the ReadOnly attribute will be di...
Table of contents 1. Start and stop service instr...