Detailed example of MySQL (5.6 and below) parsing JSON

Detailed example of MySQL (5.6 and below) parsing JSON

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!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • Basic JSON Operation Guide in MySQL 5.7
  • MySQL 5.7 JSON type usage details
  • Instructions for using JSON operation functions in Mysql5.7
  • A brief discussion on MySQL 5.7 JSON format retrieval
  • How to query json in the database in mysql5.6 and below
  • Detailed explanation of JSON series operation functions in Mysql
  • MySQL operations: JSON data type operations
  • MySQL insert json problem
  • Example analysis of the usage of the new json field type in mysql5.7

<<:  VMwarea virtual machine installation win7 operating system tutorial diagram

>>:  Installation and configuration method of vue-route routing management

Recommend

Detailed explanation of the seven data types in JavaScript

Table of contents Preface: Detailed introduction:...

JavaScript plugin encapsulation for table switching

This article shares the encapsulation code of Jav...

Solve the problem that the time zone cannot be set in Linux environment

When changing the time zone under Linux, it is al...

How to implement rounded corners with CSS3 using JS

I found an example when I was looking for a way t...

Vue implements the question answering function

1. Request answer interface 2. Determine whether ...

MySQL transaction autocommit automatic commit operation

The default operating mode of MySQL is autocommit...

Linux parted disk partition implementation steps analysis

Compared with fdisk, parted is less used and is m...

Vue implements login type switching

This article example shares the specific code of ...

Solution to the problem that Centos8 cannot install docker

Problem [root@zh ~]# [root@zh ~]# [root@zh ~]# yu...

mysql5.7.20 installation and configuration method graphic tutorial (mac)

MySQL 5.7.20 installation and configuration metho...

Difference between HTML ReadOnly and Enabled

The TextBox with the ReadOnly attribute will be di...

MySQL service and database management

Table of contents 1. Start and stop service instr...