Vqf5mG96cSTT
Vqf5mG96cSTT

Reputation: 2891

How to allow special characters in JSON_VALUE

I have a table with a VARCHAR column I use as JSON. In the column is the following data: {"Key-Name": "A value."}.

If I use JSON_VALUE to filter on this column with the query below I get the following error: "JSON path is not properly formatted. Unexpected character '-' is found at position 5.".

SELECT *
FROM [MyTable]
WHERE JSON_VALUE([Value], N'$.Key-Name') = 'A value'

How can I retrieve values with the JSON_VALUE function when keys have special characters in them?

Upvotes: 8

Views: 5066

Answers (1)

Vqf5mG96cSTT
Vqf5mG96cSTT

Reputation: 2891

You can use double quotes to escape the key name:

SELECT *
FROM [MyTable]
WHERE JSON_VALUE([Value], N'$."Key-Name"') = 'A value'

Upvotes: 13

Related Questions