我们在mysql 5.6中使用这个common_schema库来从json数组中提取值。格式如下。但它返回NULL值。那么,请你帮我们解决如何使用common_schema解析json数组。
select common_schema.extract_json_value('"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devils Food" }
]','/id');
预期输出应保存在表格中
id type
1001 Regular
1002 Chocolate
1003 Blueberry
1004 Devils Food
请告诉我们如何实现此解析。
由于 格利扬
答案 0 :(得分:0)
直接看起来并不容易得到你需要的东西。
获取单个值的选项是:
SET @`json` := '
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devils Food" }
]
}
';
SELECT
`common_schema`.`extract_json_value`(@`json`,'descendant-or-self::id[1]') `id`,
`common_schema`.`extract_json_value`(@`json`,'descendant-or-self::type[1]') `type`;
+------+---------+
| id | type |
+------+---------+
| 1001 | Regular |
+------+---------+
1 row in set (0,04 sec)