简体   繁体   中英

How to convert json object into array in prestodb/athena

I have a JSON object in this format data = {"1": {"col1":"a", "col2":"b"}, "2": {"col1":"c", "col2":"d"}....,"99":{"col1":"asd", "col2":"exm"}}. I would like to get all the values for col_1 and col_2 using athena. How do I achieve this using athena?

I was able to solve it by converting it to Map and then unnesting it like

select key, value from table where unnest(cast(json_parse(data) as Map(varchar,JSON)) as t(key,value))

Ignore that it is a JSON format (since it is not simple enough) and use the regex function:

SELECT regexp_extract_all(column_data, '"col1":"([a-z]+)",'); -- [a, c, ... asd]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM