Reputation: 4879
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')
Upvotes: 10
Views: 8280
Reputation: 13393
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'))
Upvotes: 7
Reputation: 1007
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)
Upvotes: 0
Reputation: 117
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
Upvotes: 7