MySQL JSON_EXTRACT with dynamic key name in nested level

Viewed 67

I have the following JSON object and I want to search email "abc@yopmail.com" exists in the JSON.

{
    "form_data": {
        "company": {
            "additional": {
                "cniu": {
                    "email": "abc@yopmail.com"
                }
            }
        },
        "location": {
            "additional": {
                "pdum": {
                    "email": "xyz@yopmail.com"
                }
            }
        }
    }
}

The problem which I have faced here is, at the nested level key name "cniu" is a random generated string. I have tried the following query but it does not work.

SELECT * 
FROM table as a 
WHERE 1 
AND ( CASE WHEN JSON_VALID(a.data) THEN 
            JSON_EXTRACT(a.data,'$.company.additional.*.email') 
        ELSE null END) = 'abc@yopmail.com'


SELECT * 
FROM table as a 
WHERE 1 
AND ( CASE WHEN JSON_VALID(a.data) THEN 
        JSON_EXTRACT(a.data,'$.company.additional.$.*.email') 
    ELSE null END) = 'abc@yopmail.com'
0 Answers
Related