Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 814
bdebaere Avatar asked Aug 28 '18 11:08

bdebaere


1 Answers

You can use double quotes to escape the key name:

SELECT *
FROM [MyTable]
WHERE JSON_VALUE([Value], N'$."Key-Name"') = 'A value'
like image 102
bdebaere Avatar answered Sep 28 '22 10:09

bdebaere