有没有一种方法可以将包含json数组的文本列中的数据提取到一个表中?
例如,如果我。。。
CREATE TABLE tableWithJsonStr (location TEXT, jsonStr TEXT);
INSERT INTO tableWithJsonStr VALUES
('Home', '[{"animalId":"1","type":"dog", "color":"white","isPet":"1"},{"animalId":"2","type":"cat", "color":"brown","isPet":"1"}]'),
('Farm', '[{"animalId":"8","type":"cow", "color":"brown","isPet":"0"}, {"animalId":"33","type":"pig", "color":"pink","isPet":"0"}, {"animalId":"22","type":"horse", "color":"black","isPet":"1"}]'),
('Zoo', '[{"animalId":"5","type":"tiger", "color":"stripes","isPet":"0"}]');
和
CREATE TABLE animal (
location TEXT,
idx INT,
animalId INT,
type TEXT,
color TEXT,
isPet BOOLEAN
);
我可以通过运行以下命令提取tablewithjsonstr.jsonstr:
INSERT INTO animal
SELECT location,
idx AS id,
TRIM(BOTH'"' FROM JSON_EXTRACT(jsonStr, CONCAT('$[', idx, '].animalId'))) AS animalId,
TRIM(BOTH'"' FROM JSON_EXTRACT(jsonStr, CONCAT('$[', idx, '].type'))) AS type,
TRIM(BOTH'"' FROM JSON_EXTRACT(jsonStr, CONCAT('$[', idx, '].color'))) AS color,
TRIM(BOTH'"' FROM JSON_EXTRACT(jsonStr, CONCAT('$[', idx, '].isPet'))) AS isPet
FROM tableWithJsonStr
JOIN(
SELECT 0 AS idx UNION
SELECT 1 AS idx UNION
SELECT 2 AS idx UNION
SELECT 3 AS idx
) AS indexes
WHERE JSON_EXTRACT(jsonStr, CONCAT('$[', idx, ']')) IS NOT NULL;
动物表的结果是:
| location | idx | animalId | type | color | isPet |
|==========|=====|==========|=======|=========|=======|
| Farm | 0 | 8 | cow | brown | 0 |
| Farm | 1 | 33 | pig | pink | 0 |
| Farm | 2 | 22 | horse | black | 1 |
| Home | 0 | 1 | dog | white | 1 |
| Home | 1 | 2 | cat | brown | 1 |
| Zoo | 0 | 5 | tiger | stripes | 0 |
虽然解决方案可行,但它是不可扩展的。如果我的json数组中有3个以上的对象,除非我在join中添加另一个select4作为idx,否则它们不会被计算在内。有没有更好的方法来迭代数组中的对象,而不需要预先知道每个数组中可能存在的最大对象数?
1条答案
按热度按时间wqnecbli1#
如果您使用的是mysql 8.0,那么可以使用
JSON_TABLE
命令从JSON
:输出:
在dbfiddle上演示
如果您仍然使用MySQL5.7,可以使用存储过程来提取数据:
输出:
在dbfiddle上演示