简体   繁体   中英

Presto - Extract Key in an Array

I have this sample data (from Athena)

 SELECT DISTINCT m.key
  FROM (VALUES JSON '{"name":"project1","completed":false}', JSON '{"name":"project1","completed":false}',JSON '{"name":"project1","completed":false}')
     example_table(json_column)
 CROSS JOIN UNNEST (map_keys(CAST(json_column AS map<varchar,json>))) AS m(key);

 WITH dataset AS (
  SELECT '{"name": "Susan Smith",
           "org": "engineering",
           "projects": [{"name":"project1", "completed":false},
           {"name":"project2", "completed":true}]}'
    AS blob
)
select * from dataset

This will generate the below output.

{"name": "Susan Smith", "org": "engineering", "projects": [{"name":"project1", "completed":false}, {"name":"project2", "completed":true}]}

I want to extract Key from the output.

Expected output:

output
------
name
org
projects

Could someone help on this?

updated:

edited this question with proper JSON.

Based on discussion in comments, this

map_keys(CAST(json_column AS map<varchar,json>))

needs to be replaced with

map_keys(CAST(CAST(json_column AS JSON) AS map<varchar,json>))

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