Search code examples
jsonsql-serverwhere-clausewhere-in

How to use JSON_VALUE in where in clause (SQL Server)


: am trying to use part of a JSON to use as a filter for a where in clause as follows:

declare @json nvarchar(max) = 
'[{
  "brand_name": {
    "key": "Brand Name",
    "value": "''alpha'',''omega''"
  }
}]';

select *
from someTable
where column in (select json_value(@json, '$[0].brand_name.value'));

How do I convert the value into a where-in argument?


Solution

  • Like this:

    declare @json nvarchar(max) = 
    '[{
      "brand_name": {
        "key": "Brand Name",
        "value": ["alpha","omega"]
      }
    }]';
    
    select value from openjson(@json, '$[0].brand_name.value')