从 Json 数组中提取 JSON 对象到 mysql 8
Posted
技术标签:
【中文标题】从 Json 数组中提取 JSON 对象到 mysql 8【英文标题】:Extract JSON Object from Json Array into mysql 8 【发布时间】:2021-11-27 09:54:04 【问题描述】:[""delivered":0,"deliveryDate":1633537686480,"toUserId":148",""delivered":0,"deliveryDate":1633537687590,"toUserId":226"," "delivered":1,"deliveryDate":1633537687741,"toUserId":160",""delivered":0,"deliveryDate":1633537687863,"toUserId":262",""delivered":0, "deliveryDate":1633537688019,"toUserId":263",""delivered":0,"deliveryDate":1633537688174,"toUserId":264",""delivered":0,"deliveryDate":1633537688325, "toUserId":265"]
我想从下表中提取此 JSON 数组,然后进一步提取我提供 userId 和 deliveryStatus 的 JSON 对象。我该如何实现?
预期结果 当我给 toUserid 148 并且还给了 deliveryStatus= 0
"delivered":0,"deliveryDate":1633537686480,"toUserId":148
【问题讨论】:
【参考方案1】:您可以使用JSON_TABLE
函数将JSON转换为行;然后使用->
运算符过滤:
SELECT t.id, JSON_PRETTY(j.obj)
FROM t
CROSS JOIN JSON_TABLE(t.delivery_status, '$[*]' COLUMNS(
obj JSON PATH '$'
)) AS j
WHERE j.obj->'$.delivered' = 0 AND j.obj->'$.toUserId' = 148
【讨论】:
谢谢!为了您的快速回复。对不起,我对 Json 进行了一些更改,例如 ["\"delivered\":0,\"deliveryDate\":1633537686480,\"toUserId\":148","\"delivered\" :0,\"deliveryDate\":1633537687590,\"toUserId\":226","\"delivered\":1,\"deliveryDate\":1633537687741,\"toUserId\":160"," \"delivered\":0,\"deliveryDate\":1633537687863,\"toUserId\":262","\"delivered\":0,\"deliveryDate\":1633537688019,\"toUserId\" :263","\"delivered\":0,\"deliveryDate\":1633537688174,\"toUserId\":264","\"delivered\":0,\"deliveryDate\":1633537688325 ,\"toUserId\":265"] 现在我怎样才能做到这一点,我只想返回结果? 在我发布的查询中更改列名并不难。 不,我的问题是其他问题,我知道更改列名并不难,但谢谢!! 我的问题是,现在我有一个 JSON 数组,但 JsonObject 充当字符串类型,现在由于字符串类型,我无法提取数据,所以这是我的问题以上是关于从 Json 数组中提取 JSON 对象到 mysql 8的主要内容,如果未能解决你的问题,请参考以下文章