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
Docker runs multiple Springboot First: Port mappi...
You always need data for development. As a server...
Preface: MySQL is a relational database managemen...
Preface This article mainly explains how to imple...
In actual development, the primary key of MySQL c...
Phenomenon: Run an image, for example, ubuntu14.0...
There are too many articles about xhtml+css websi...
need: Merge identical items of one field and sort...
This article mainly introduces how to integrate T...
Redis is an open source NoSQL database written in...
Table of contents 1. Pull the image 1.1 Pull the ...
FFMPEG 3.4.1 version parameter details Usage: ffm...
By default, MySQL character types are not case-se...
Note: This article has been translated by someone ...
WML (Wireless Markup Language). It is a markup la...