如何解析 json 对象 - mysql



>我有一个这样的json对象:

{"1": {"penalty_percent": 3, "free_day": 5, "free_hours": 24}, "2": {"penalty_percent": 2, "free_day": 5, "free_hours": 12}, "3": {"penalty_percent": 2, "free_day": 2, "free_hours": 36}, "4": {"penalty_percent": 3, "free_day": 3, "free_hours": 48}, "5": {"penalty_percent": 5, "free_day": 2, "free_hours": 1}, "18": {"penalty_percent": 5, "free_day": 2}, "30": {"penalty_percent": 5, "free_day": 5, "free_hours": 10}}

我想从键对象中获取free_day值是2.(这里5(。

使用JSON_EXTRACT

SELECT JSON_EXTRACT(JSON_EXTRACT(json, '$."2"'), "$.free_day")
FROM yourTable;

请注意,由于您使用数字作为 JSON 键,因此需要用双引号对它们进行转义。

演示

使用MysqlJSON_EXTRACT,别名运算符为->

做这样的事情:

SELECT your_json_column->"$."2".free_day"
FROM your_table
WHERE id = 1

更多详细信息:参考

最新更新