【问题标题】:How to Extract JSON Array from MySQL如何从 MySQL 中提取 JSON 数组
【发布时间】:2017-11-28 00:14:49
【问题描述】:

我打算将一个 JSON 数组插入到我的数据库表中以用于动态目的。我在提取带有键/值对的 JSON 对象时没有任何问题,但对于 JSON 数组则相反。见下例:

[{"LCM": {"id": 333, "barcode": "ABC"}, "DCover": {"id": 444, "barcode": "CDE"}, "date_associated": "2017-11-27 23:59:59"}, {"LCM": {"id": 555, "barcode": "EFG"}, "DCover": {"id": 666, "barcode": "GHI"}, "date_associated": "2017-11-27 23:59:59"}, {"LCM": {"id": 777, "barcode": "IJK"}, "DCover": {"id": 888, "barcode": "KLM"}, "date_associated": "2017-11-27 23:59:59"}]

理想情况下,根据上面的每一个,如果提取 - 它看起来像这样:

{"LCM": {"id": 777, "barcode": "IJK"}, "DCover": {"id": 888, "barcode": "KLM"}, "date_associated": "2017-11-27 23:59:59"}

现在的问题是,我该如何提取它们:

1. {"LCM": {"id": 333, "barcode": "ABC"}, "DCover": {"id": 444, "barcode": "CDE"}, "date_associated": "2017-11-27 23:59:59"}
2. {"LCM": {"id": 555, "barcode": "EFG"}, "DCover": {"id": 666, "barcode": "GHI"}, "date_associated": "2017-11-27 23:59:59"}
3. {"LCM": {"id": 777, "barcode": "IJK"}, "DCover": {"id": 888, "barcode": "KLM"}, "date_associated": "2017-11-27 23:59:59"}

通过它们中的哪一个,我将能够根据需要进行解析,即:“$.LCM”或“$.DCover.barcode”

JSON_EXTRACT(...) 似乎只能通过定义特定 JSON 对象的键来使用。但这不适用于纯值 JSON 数组(据我所知)

希望,我可以在这里得到一个小费。谢谢。

【问题讨论】:

    标签: mysql json


    【解决方案1】:

    如果你使用 MySQL >= 8.0.4,你可以使用JSON_TABLE

    查询示例

    with tbl(val) as (
      select CAST('[{"LCM": {"id": 333, "barcode": "ABC"}, "DCover": {"id": 444, "barcode": "CDE"}, "date_associated": "2017-11-27 23:59:59"}, {"LCM": {"id": 555, "barcode": "EFG"}, "DCover": {"id": 666, "barcode": "GHI"}, "date_associated": "2017-11-27 23:59:59"}, {"LCM": {"id": 777, "barcode": "IJK"}, "DCover": {"id": 888, "barcode": "KLM"}, "date_associated": "2017-11-27 23:59:59"}]'
      as json
     )
    )
    select rowid, content from tbl, JSON_TABLE(val, '$[*]' columns (rowid for ordinality, content json PATH '$')) as jt;
    

    结果

    +-------+---------------------------------------------------------------------------------------------------------------------------+
    | rowid | content                                                                                                                   |
    +-------+---------------------------------------------------------------------------------------------------------------------------+
    |     1 | {"LCM": {"id": 333, "barcode": "ABC"}, "DCover": {"id": 444, "barcode": "CDE"}, "date_associated": "2017-11-27 23:59:59"} |
    |     2 | {"LCM": {"id": 555, "barcode": "EFG"}, "DCover": {"id": 666, "barcode": "GHI"}, "date_associated": "2017-11-27 23:59:59"} |
    |     3 | {"LCM": {"id": 777, "barcode": "IJK"}, "DCover": {"id": 888, "barcode": "KLM"}, "date_associated": "2017-11-27 23:59:59"} |
    +-------+---------------------------------------------------------------------------------------------------------------------------+
    

    【讨论】:

      【解决方案2】:
       According to my understand you are using JSON Array, use iteration
      
       CREATE PROCEDURE `Manipulation_Json_Data`()
        LANGUAGE SQL
        NOT DETERMINISTIC
        CONTAINS SQL
        SQL SECURITY DEFINER
        COMMENT ''
        BEGIN
        DECLARE v1 INT DEFAULT 0;
      
        SET @j = '[{"LCM": {"id": 333, "barcode": "ABC"}, "DCover": {"id": 444, "barcode": "CDE"}, "date_associated": "2017-11-27 23:59:59"}, {"LCM": {"id": 555, "barcode": "EFG"}, "DCover": {"id": 666, "barcode": "GHI"}, "date_associated": "2017-11-27 23:59:59"}, {"LCM": {"id": 777, "barcode": "IJK"}, "DCover": {"id": 888, "barcode": "KLM"}, "date_associated": "2017-11-27 23:59:59"}]';
      
         WHILE v1 < 4 DO
           SET @data_v = JSON_EXTRACT(@j, concat('$[',v1,']'));
           INSERT INTO database.table_name (`ID`, `Text`) VALUES (NULL, @data_v );
           SET v1 = v1 + 1;
         END WHILE;
        END
      

      【讨论】:

      • 这是纯粹在 MySQL 中完成的吗?因为目标是完全在 MySQL(数据库服务器)中独立完成
      • 更新答案请查看。这将像这样保存行: 1. {"LCM": {"id": 333, "barcode": "ABC"}, "DCover": {"id": 444, "barcode": "CDE"}, “日期关联”:“2017-11-27 23:59:59”} 2. {“LCM”:{“id”:555,“条形码”:“EFG”},“DOver”:{“id”:666 , "barcode": "GHI"}, "date_associated": "2017-11-27 23:59:59"} 3. {"LCM": {"id": 777, "barcode": "IJK"}, "DCover": {"id": 888, "barcode": "KLM"}, "date_associated": "2017-11-27 23:59:59"}
      • 嗯,我正在寻找一个 MySQL 服务器端的解决方案。不涉及任何语言细节。
      猜你喜欢
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 2016-07-20
      • 1970-01-01
      • 1970-01-01
      • 2021-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多