簡體   English   中英

如何從MySQL提取JSON數組

[英]How to Extract JSON Array from MySQL

我打算出於動態目的將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對象的鍵來使用JSON_EXTRACT( ... )。 但這不適用於僅值JSON數組(據我所知)

希望我能在這里給小費。 謝謝。

如果您使用的是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"} |
+-------+---------------------------------------------------------------------------------------------------------------------------+
 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM