Extract values from a JSON Array in Presto

Viewed 2768

I have a column with JSON arrays like below:

{data=[{"name":"col1","min":0,"max":32,"avg":29},
{"name":"col2","min":1,"max":35,"avg":21},
{"name":"col3","min":4,"max":56,"avg":34}]}

I'm trying to parse the array and extract specific values based on conditions. For example

value of "min" where "name"="col1": 0

value of "avg" where "name"="col3": 34

Does anyone have a solution for that?

1 Answers

Your JSON is not valid one. it should be {"data":[ not {data = [

If JSON is valid ( you can easily fix it in a subquery ), extract data, cast it to array(row) and get values using CASE expressions. I added max() aggregation here to remove NULL records and get all required values in single row, you can use filter instead (for example where x.name = 'col1'), depending on what you need:

with mydata as (
select '{"data":[{"name":"col1","min":0,"max":32,"avg":29},
{"name":"col2","min":1,"max":35,"avg":21},
{"name":"col3","min":4,"max":56,"avg":34}]}' json
)

select max(case when x.name = 'col1' then x.min end) min_col1,
       max(case when x.name = 'col3' then x.avg end) avg_col3
from mydata
CROSS JOIN
    UNNEST(
            CAST(
                JSON_EXTRACT(json,'$.data')
                    as ARRAY(ROW(name VARCHAR, min INTEGER, max INTEGER, avg INTEGER))
                 )
          ) as x(name, min, max, avg) --column aliases

Result:

min_col1    avg_col3
0           34
Related