0

I'm working with JSON values in a database for the first time. I want to use JSON_VALUE or JSON_QUERY to return a value or section of the JSON, but whoever designed this went and used '-' in the keys, which is an illegal value. Research suggests I need to use FOR JSON to escape it, but I can't figure out how.

Attempt at the query:

select 
    xt.ID,
    JSON_QUERY(xt.JSON_DB_VALUE, '$.CusomterQuery.Details.cust-names') as JSON_WITH_NAMES,
    JSON_VALUE(xt.JSON_DB_VALUE, '$.CusomterQuery.Details.cust-names.first-name') as FIRST_NAME
from EXAMPLE_TABLE xt 

Error received:

JSON path is not properly formatted. Unexpected character '-' is found at position xx.

Thanks

2
  • 1
    Quote the path: $.CusomterQuery.Details."cust-names".
    – Zhorov
    Commented Apr 7, 2022 at 8:55
  • yep, that's all I needed. Thank you again!
    – Steve
    Commented Apr 7, 2022 at 9:05

1 Answer 1

0

thanks to Zhorov's quick comment, this is the very simple solution - add quotation marks around the parts of the path with the illegal character.

select 
    xt.ID,
    JSON_QUERY(xt.JSON_DB_VALUE, '$.CusomterQuery.Details."cust-names"') as JSON_WITH_NAMES,
    JSON_VALUE(xt.JSON_DB_VALUE, '$.CusomterQuery.Details."cust-names"."first-name"') as FIRST_NAME
from EXAMPLE_TABLE xt 

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