0

I have a JSON field in MySQL with the following structure:

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

I want to get records where 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 ? Are you not able to use numbers as keys in your JSON structure?

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.