sql—从json字段mysql中的键、值对列表中提取一个键

f87krz0w  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(442)

我有以下格式的数据:

"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)"}
            ]}
    ],

我想拔出钥匙

article_desc

article_body .
我在mysql中的代码片段:

SELECT 

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

我得到的是空数据,如何解析这些数据?

4nkexdtk

4nkexdtk1#

使用json密钥
如。

SELECT JSON_KEYS(full_article_json) as jsonKeys;

它将返回json数组中的所有键

of1yzvn4

of1yzvn42#

你的 key = 'article_body' 是一个数组json,所以需要使用index获取数据。
你可以试试这个。
架构(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)"}
        ]}
]}');

查询#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" |

db fiddle视图
如果你想从 article_desc 从哪来的 article_body 数组。你可以试着用 * 在索引中。

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

相关问题