I am trying to query a column that contains an array of json objects.
The array of object looks like this:
[
{
"TYPE": "car",
"NAME": "lucy"
},
{
"TYPE": "bus",
"NAME": "bob"
},
{
"TYPE": "car",
"NAME": "mary"
}
]
I am trying to select if the column contains objects with type = "car" and name = "lucy"
You can use OPENJSON
to break out the array into separate rows:
SELECT j.*
FROM YourTable t
CROSS APPLY OPENJSON(t.) WITH (
[TYPE] varchar(50),
[NAME] varchar(50)
) j
WHERE j.[TYPE] = 'car' AND j.[NAME] = 'lucy'
Or if you want to just filter the main table based on that, put it inside a WHERE
SELECT t.*
FROM YourTable t
WHERE EXISTS (SELECT 1
FROM OPENJSON(t.) WITH (
[TYPE] varchar(50),
[NAME] varchar(50)
) j
WHERE j.[TYPE] = 'car' AND j.[NAME] = 'lucy'
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.