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 tutorial on running multiple Springboot with Docker

Docker runs multiple Springboot First: Port mappi...

Detailed installation tutorial of mysql 5.7 under CentOS 6 and 7

You always need data for development. As a server...

mysql5.6.zip format compressed version installation graphic tutorial

Preface: MySQL is a relational database managemen...

Detailed explanation of Linux curl form login or submission and cookie usage

Preface This article mainly explains how to imple...

How to keep running after exiting Docker container

Phenomenon: Run an image, for example, ubuntu14.0...

Use PS to create an xhtml+css website homepage in two minutes

There are too many articles about xhtml+css websi...

SQL group by to remove duplicates and sort by other fields

need: Merge identical items of one field and sort...

Vue integrates Tencent TIM instant messaging

This article mainly introduces how to integrate T...

Docker installs redis 5.0.7 and mounts external configuration and data issues

Redis is an open source NoSQL database written in...

Detailed tutorial on installing SonarQube using Docker

Table of contents 1. Pull the image 1.1 Pull the ...

Detailed description of ffmpeg Chinese parameters

FFMPEG 3.4.1 version parameter details Usage: ffm...

MySQL character types are case sensitive

By default, MySQL character types are not case-se...

The forgotten button tag

Note: This article has been translated by someone ...

What is WML?

WML (Wireless Markup Language). It is a markup la...