0
{
    "NameDetailInfo": [{
        "cName": "Tony",
        "sex": "M"
    }, {
        "cName": "Lucy",
        "sex": "M"
    }, {
        "cName": "Lily",
        "sex": "M"
    }, {
        "cName": "Ben",
        "sex": "M"
    }, {
        "cName": "Daa",
        "sex": "F"
    }, {
        "cName": "Kode",
        "sex": "F"
    }, {
        "cName": "Andy",
        "sex": "F"
    }, {
        "cName": "Koya",
         "sex": "F"
    },{
        "cName": "Kay",
        "sex": "M"
    }]
}

I want select this array cName.

select *
from CD_Name_List
where JSON_VALUE(CD_Name_List.MemberJSON,'$.NameDetailInfo[0].cName') = 'Kay'

This just select NameDetailInfo[0], but I want select all array.

1 Answer 1

4

You can use OPENJSON to break out the array

SELECT j.cName, j.sex
FROM CD_Name_List nl
CROSS APPLY OPENJSON (nl.MemberJSON, '$.NameDetailInfo')
  WITH (cName nvarchar(100), sex char(1)) j
WHERE j.cName = 'Kay';

Or, if you meant that you want to select the whole array if such an element exists, you can use OPENJSON in a subquery

SELECT nl.*
FROM CD_Name_List nl
WHERE EXISTS (SELECT 1
    FROM OPENJSON (nl.MemberJSON, '$.NameDetailInfo')
      WITH (cName nvarchar(100)) j
    WHERE j.cName = 'Kay'
);

db<>fiddle

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.