从json字段mysql

时间:2018-09-24 07:06:23

标签: mysql sql json

我有以下格式的数据:

"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;

我正在获取空数据,如何解析此类数据?

2 个答案:

答案 0 :(得分:1)

您的key = 'article_body'是一个数组JSON,因此您需要使用索引来获取数据。

您可以尝试一下。

模式(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" |

View on DB Fiddle

如果要从article_desc数组中获取article_body的所有值。您可以尝试在索引中使用*

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

答案 1 :(得分:0)

使用JSON_KEYS

例如

<div class="form-group input-group">
    <span class="input-group-addon">Name</span>
        <input type="text" name="name" class="form-control" placeholder="MyModel Name">
</div>

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