简体   繁体   English

从json字段mysql中的键,值对列表中提取键

[英]Extract a key from list of key,values pairs in json field mysql

I have a data in the following format: 我有以下格式的数据:

"article_body" : [
        {
            "article_desc" : "THURSDAY, Sept. 1, 2016 (HealthDay News) -- Dapagliflozin improves insulin sensitivity and increases lipid oxidation and plasma ketone concentration in patients with type 2 diabetes mellitus (T2DM), according to a study published online Aug. 25 in Diabetes Care. \n\n Giuseppe Daniele",
            "links" : [{
                    "link_name" : "Full Text (subscription or payment may be required)"}
            ]}
    ],

I want to extract the key 我想提取密钥

article_desc

from article_body . 来自article_body

My Snippet of code in Mysql: 我在Mysql中的代码片段:

SELECT 

    JSON_EXTRACT(full_article_json, '$.article_body."article_desc"') AS description,
FROM
    wc_article_full_data;

I'm getting null data, how to parse such data? 我得到空数据,如何解析这些数据?

Your key = 'article_body' is an array JSON, so you need to use index get the data. 你的key = 'article_body'是一个数组JSON,所以你需要使用索引获取数据。

You can try this. 你可以试试这个。

Schema (MySQL v5.7) 架构(MySQL v5.7)

CREATE TABLE wc_article_full_data(

   full_article_json JSON
);

insert into wc_article_full_data values (
'{"article_body" : [
    {
        "article_desc" : "THURSDAY, Sept. 1, 2016 (HealthDay News) -- Dapagliflozin improves insulin sensitivity and increases lipid oxidation and plasma ketone concentration in patients with type 2 diabetes mellitus (T2DM), according to a study published online Aug. 25 in Diabetes Care.  Giuseppe Daniele",
        "links" : [{
                "link_name" : "Full Text (subscription or payment may be required)"}
        ]}
]}');

Query #1 查询#1

SELECT JSON_EXTRACT(full_article_json,'$.article_body[0].article_desc') AS descriptio
FROM wc_article_full_data;

| descriptio                                                                                                                                                                                                                                                                                 |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| "THURSDAY, Sept. 1, 2016 (HealthDay News) -- Dapagliflozin improves insulin sensitivity and increases lipid oxidation and plasma ketone concentration in patients with type 2 diabetes mellitus (T2DM), according to a study published online Aug. 25 in Diabetes Care.  Giuseppe Daniele" |

View on DB Fiddle 查看DB小提琴

If you want to get all value from article_desc which from article_body array. 如果你想从article_desc中获取article_body所有值。 you can try to use * in index. 你可以尝试在索引中使用*

SELECT JSON_EXTRACT(full_article_json,'$.article_body[*].article_desc') AS descriptio
FROM wc_article_full_data

use JSON_KEYS 使用JSON_KEYS

eg. 例如。

SELECT JSON_KEYS(full_article_json) as jsonKeys;

it will return all the keys from the json array 它将返回json数组中的所有键

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM