Using Presto SQL, the original format of the column appears like this "{"ORDERS (completed)": 1000.00, "ORDERS (failed) USD": 3000.00}"
and I am trying to extract the key and value by parsing and casting as map (varchar, double) and then unnesting
UNNEST(
CAST(JSON_PARSE(json) as MAP<VARCHAR, DOUBLE>)
) x(key, value)
When I use json_parse
, the forward slashes and quotes disappear, but I receive an error (Expected a json object) when trying to cast as map<varchar, double>
{"ORDERS (filled)": 1000.00, "ORDERS (succeeded) USD": 3000.00}
I’ve also tried casting as json, but received a similar error
"{"ORDERS (completed) USD": 1000.0}" ""{\"ORDERS (failed) USD\": 3000.0}""
2
Answers
The solution to my problem:
We want to be careful to not remove all of the double quotes for
json_parse
to work, just need the double quotes surrounding the json string. So we usesubstr(col, 2, length(col) - 2)
to get the string inside of the quotes. We will need to replace the forward slashes with '' forjson_parse
to convert to json. Then we can usejson_parse
andcast as map<type, type>
as usual.Your JSON is a string that contains encoded JSON (basically it is a double-encoded JSON), so parsing it one time will not do the trick. The correct approach would be to double parse it:
Which gives the following output: