9

I have a table [JsonTable], and the column [JsonData] save the json string,

JsonData like:

{
   "Names": ["John", "Joe", "Sam"]
}

How can I inner join this table like:

SELECT* FROM [TestTable] AS T
INNER JOIN [JsonTable] AS J ON T.[Name] IN JSON_QUERY(J.[JsonData], '$.Names')
Serkan Arslan
  • 13,158
  • 4
  • 29
  • 44
Max
  • 4,439
  • 2
  • 18
  • 32

3 Answers3

7

You need to use OPENJSON function for reading Names array. You can use this query.

SELECT * FROM [TestTable] T
INNER JOIN [JsonTable] AS J ON T.[Name] IN (SELECT value FROM OPENJSON(J.[JsonData],'$.Names'))
wchatx
  • 187
  • 2
  • 8
Serkan Arslan
  • 13,158
  • 4
  • 29
  • 44
7

Another way is to use the cross apply operator like this:

SELECT *
FROM [JsonTable] AS J
CROSS APPLY OPENJSON(J.[JsonData], '$.Names') jsonValue
INNER JOIN [TestTable] T ON T.[Name] = jsonvalue.value  
Lydia
  • 117
  • 4
-1

You can use JSON_VALUE to get particular field value,

SELECT * 
FROM [TestTable] AS T 
INNER JOIN [JsonTable] AS J 
ON T.[Name] IN JSON_VALUE(J.[JsonData], '$.Names');

for the nested value have look this link : JSON_VALUE (Transact-SQL)

monikapatelIT
  • 977
  • 14
  • 26