skip to Main Content

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


  1. Chosen as BEST ANSWER

    The solution to my problem:

    cast(json_parse(replace(substr(col, 2, length(col) - 2), '', '')) as map<varchar, double>)
    

    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 use substr(col, 2, length(col) - 2) to get the string inside of the quotes. We will need to replace the forward slashes with '' for json_parse to convert to json. Then we can use json_parse and cast as map<type, type> as usual.


  2. 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:

    with dataset(col) as (
        values ('"{"ORDERS (completed)": 1000.00, "ORDERS (failed) USD": 3000.00}"')
    )
    
    select cast(
               json_parse(
                   json_extract_scalar(
                       json_parse(col), '$')) as map(varchar, double))
    from dataset;
    

    Which gives the following output:

                              _col0                          
    ---------------------------------------------------------
     {ORDERS (failed) USD=3000.0, ORDERS (completed)=1000.0} 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search