10

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')

3 Answers 3

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'))
Sign up to request clarification or add additional context in comments.

Comments

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  

Comments

0

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)

1 Comment

Incorrect syntax near 'JSON_VALUE'. Expecting '('.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.