在mysql中json元素到行或json到表行(在mysql中导出json)

7rtdyuoh  于 2021-06-17  发布在  Mysql
关注(0)|答案(2)|浏览(386)

我们可以有许多请求来将json存储到数据库中。有时我们需要将json键集转换为表行。如何在mysql表中提取完整的json?
i、 e.具有以下json和要求是将每个集合存储为表中的一行。

'{
"log": [
{
"datetime": "2017-10-25 07:19:55",
"Activity": "Activity 1"
},
{
"datetime": "2017-10-25 07:20:05",
"Activity": "Activity 2"
},
{
"datetime": "2017-10-25 07:20:31",
"Activity": "Activity 3"
},
{
"datetime": "2017-10-25 07:20:31",
"Activity": "Activity 4"
},
{
"datetime": "2017-10-25 07:21:03",
"Activity": "Activity 5"
},
{
"datetime": "2017-10-25 13:56:42",
"Activity": "Activity 6"
},
{
"datetime": "2017-10-25 13:56:53",
"Activity": "Activity 7"
},
{
"datetime": "2017-10-25 13:57:03",
"Activity": "Activity 8"
},
{
"datetime": "2017-10-25 13:57:04",
"Activity": "Activity 9"
}, 
{ "datetime": "2017-10-25 13:57:53",
"Activity": "Activity 10"
},
{
"datetime": "2017-10-25 13:57:59",
"Activity": "Activity 11"
},
{
"datetime": "2017-10-25 13:58:01",
"Activity": "Activity 12"
},
{
"datetime": "2017-10-25 13:58:01",
"Activity": "Activity 13"
}
]
} '
uajslkp6

uajslkp61#

json可以存储在具有不同数据类型的数据库中。i、 例如json、vacrhar、文本等。对于导出报表或过滤json元素值,我们应该有不同的解决方案来完成。
创建一个表来在mysql表中存储json。

CREATE TABLE test_Json(id INT, activity_log JSON);

将示例数据插入表中。json字段数据插入到json数据类型中,但也可以是varchar或text类型。

INSERT INTO test_Json(id, activity_log)
VALUES(1,
'{
"log": [
{
"datetime": "2017-10-25 07:19:55",
"Activity": "Activity 1"
},
{
"datetime": "2017-10-25 07:20:05",
"Activity": "Activity 2"
},
{
"datetime": "2017-10-25 07:20:31",
"Activity": "Activity 3"
},
{
"datetime": "2017-10-25 07:20:31",
"Activity": "Activity 4"
},
{
"datetime": "2017-10-25 07:21:03",
"Activity": "Activity 5"
},
{
"datetime": "2017-10-25 13:56:42",
"Activity": "Activity 6"
},
{
"datetime": "2017-10-25 13:56:53",
"Activity": "Activity 7"
},
{
"datetime": "2017-10-25 13:57:03",
"Activity": "Activity 8"
},
{
"datetime": "2017-10-25 13:57:04",
"Activity": "Activity 9"
}, { "datetime": "2017-10-25 13:57:53",
"Activity": "Activity 10"
},
{
"datetime": "2017-10-25 13:57:59",
"Activity": "Activity 11"
},
{
"datetime": "2017-10-25 13:58:01",
"Activity": "Activity 12"
},
{
"datetime": "2017-10-25 13:58:01",
"Activity": "Activity 13"
}
]
} ');

INSERT INTO test_Json(id, activity_log)
VALUES(2,'
{
"log": [
{ "datetime": "2017-10-29 21:56:27",
"Activity": "Activity-11"
},
{
"datetime": "2017-10-29 21:56:27",
"Activity": "Activity-12"
},
{
"datetime": "2017-10-29 21:56:27",
"Activity": "Activity-13"
},
{
"datetime": "2017-10-29 21:56:27",
"Activity": "Activity-14"
},
{
"datetime": "2017-10-29 21:56:35",
"Activity": "Activity-15"
},
{
"datetime": "2017-10-29 21:56:49",
"Activity": "Activity-16"
},
{
"datetime": "2017-10-29 21:56:49",
"Activity": "Activity-17"
},
{
"datetime": "2017-10-29 21:56:49",
"Activity": "Activity-18"
}
]
}');

查询以将json导出为json长度值为的json数组

SELECT TJ.id,
JSON_LENGTH(JSON_EXTRACT(TJ.activity_log, '$**.datetime')) as Json_Length,
JSON_EXTRACT(TJ.activity_log, '$**.datetime') as json_array_message,
JSON_EXTRACT(TJ.activity_log, '$**.Activity') as json_array_datetime
FROM test_Json TJ
WHERE JSON_VALID(TJ.activity_log) = 1;
``` `JSON_VALID()` 将验证json列数据,无论json数据是否正确。否则,它将从查询中筛选出来。
递归cte(公共表表达式)以获取行格式的n级元素值

WITH RECURSIVE CTE_json (id, array_datetime, array_message,seq) AS
(
SELECT id,
JSON_EXTRACT(JSON_EXTRACT(activity_log, '$.datetime'), CONCAT('$[', 0, ']')),
JSON_EXTRACT(JSON_EXTRACT(activity_log, '$
.Activity'), CONCAT('$[', 0, ']')),
0 as seq
FROM test_Json
UNION ALL
SELECT TJ.id,
JSON_EXTRACT(JSON_EXTRACT(TJ.activity_log, '$.datetime'), CONCAT('$[', rc.seq +1, ']')) as array_datetime,
JSON_EXTRACT(JSON_EXTRACT(TJ.activity_log, '$
.Activity'), CONCAT('$[', rc.seq +1, ']')) as array_message,
rc.seq +1 AS seq
FROM CTE_json rc
INNER JOIN test_Json TJ ON rc.id = TJ.id
WHERE rc.seq+1 < JSON_LENGTH(JSON_EXTRACT(TJ.activity_log, '$**.datetime'))
)

SELECT *
FROM CTE_json cj
ORDER BY id, seq;

如果当前的mysql版本不支持递归cte,那么我们可以使用其他循环替代方案。
uttx8gqw

uttx8gqw2#

mysql 8.0有一个json表函数:

INSERT INTO NewTable 
  SELECT * FROM JSON_TABLE('...your JSON ...', '$.log[*]' columns(
    rowid for ordinality, 
    dt datetime path '$.datetime', 
    activity varchar(20) path '$.Activity')
  ) as t

相关问题