0

I have a table with a single row of data which has a nvarchar(MAX) type column, lets name the column as json_column and the values of the column are like below:

{
"data": [
    {
        "name": "john",
        "num": "123"
    },
    {
        "name": "peter",
        "num": "345"
    },
    {
        "name": "donald",
        "num": "789"
    },
 ]
}

Now how can I get the value of num where name is passed as input to a where clause or query?

My query should return only the value of num.

For example: lets say I need the value of num where name is 'john' and the query should return me 123.

Karthik
  • 629
  • 2
  • 8
  • 12

1 Answers1

0

I just found the answer and the query can be written as follows:

SELECT OPENJSON((SELECT json_column From json_column_table),'$.data')
WITH 
(
 name nvarchar(100) '$.name',
 num  nvarchar(100) '$.num'
)
WHERE name='john';
Karthik
  • 629
  • 2
  • 8
  • 12