8

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?

1 Answer 1

13

You can use double quotes to escape the key name:

SELECT *
FROM [MyTable]
WHERE JSON_VALUE([Value], N'$."Key-Name"') = 'A value'
1
  • For anyone who stumbles on this, the quotes will also handle situations where the JSON has $ in the key name, too. Commented Jun 15, 2020 at 21:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.