I have a "~" in my json fields, such as "~id". Using Presto 0.75, I am unable to access such fields. Following is what I have tried so far without success:
SELECT json_extract_scalar('{"id":"1","table":"test"}', '$.table'); // This works
SELECT json_extract_scalar('{"id":"1","~table":"test"}', '$.[\"~table\"]'); // Doesn't work
SELECT json_extract_scalar('{"id":"1","~table":"test"}', '$.[\~table]'); // Doesn't work
Error given is "Invalid JSON path:"
Presto has several functions for reading JSON strings. To extract a JSON parameter, you can use the json_extract_scalar function.
JSON_EXTRACT_SCALAR(json_expr[, json_path]) Description. Extracts a scalar value and then returns it as a string. A scalar value can represent a string, number, or boolean. Removes the outermost quotes and unescapes the return values.
The correct form for that JSON path is: '$["~table"]'
:
presto> SELECT json_extract_scalar('{"id":"1","~table":"test"}', '$["~table"]');
_col0
-------
test
(1 row)
Here are some facts to help you understand why the alternatives you tried don't work:
'don''t'
is the SQL string literal for don't
. Double quotes within a SQL string literal do not need to be escaped.'$["foo"]'
or '$.foo'
. The field access syntax only works for names that are valid identifiers (alphanumeric and underscores).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With