2

I have a table with a JSON text field:

create table breaches(breach_id int, detail text);
insert into breaches values
( 1,'[{"breachedState": null}, 
      {"breachedState": "PROCESS_APPLICATION",}]')

I'm trying to use MSSQL's in build JSON parsing functions to test whether ANY object in a JSON array has a matching member value.

If the detail field was a single JSON object, I could use:

select * from breaches 
 where JSON_VALUE(detail,'$.breachedState') = 'PROCESS_APPLICATION'

but it's an Array, and I want to know if ANY Object has breachedState = 'PROCESS_APPLICATION'

Is this possible using MSSQL's JSON functions?

1 Answer 1

1

You can use function OPENJSON to check each object, try this query:

select * from breaches 
where exists
(
    select * 
    from
    OPENJSON (detail) d
    where JSON_VALUE(value,'$.breachedState') = 'PROCESS_APPLICATION'
)

Btw, there is an extra "," in your insert query, it should be:

insert into breaches values
( 1,'[{"breachedState": null}, 
      {"breachedState": "PROCESS_APPLICATION"}]')
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.