简体   繁体   English

在 MySQL 中查询嵌套的 JSON 字段

[英]Querying a nested JSON field in MySQL

I have a JSON field in MySQL with the following structure:我在 MySQL 中有一个 JSON 字段,其结构如下:

{
    "menu": {
        "reports": {
            "checked": 1
        }
    },
    "survey": {
        "5": {
            "checked": 1
        }
    },
    "location": {
        "208": {
            "checked": 1,
            "satisfied": 1
        }
    }
}

I want to get records where survey.5.checked=1.我想获得survey.5.checked=1的记录。
The following works fine:以下工作正常:

select email,filters,filters->'$.survey' as survey from users

and this works:这有效:

select email,filters,filters->'$.survey.reports' as reports from users

but this fails:但这失败了:

select email,filters,filters->'$.survey.5' as survey from users

and this fails:这失败了:

select email,filters,filters->'$.survey.5.checked' as survey from users

How can I query the JSON field for records where survey.5.checked = 1 ?如何在 JSON 字段中查询survey.5.checked = 1 的记录? Are you not able to use numbers as keys in your JSON structure?您不能在 JSON 结构中使用数字作为键吗?

You need to double-quote the all-digit key (similarily, if the key contains spaces, it also needs to be double-quoted).您需要双引号全数字键(同样,如果键包含空格,也需要双引号)。

select email, filters, filters -> '$.survey."5".checked' from users

If you actually want to filter:如果你真的想过滤:

select email, filters
from users
where filters -> '$.survey."5".checked' = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM