Flattening JSON column from Athena table

There is an Athena table in which 2 of those columns contains values as json data.

The requirement is to flatten the column.

I am currently following the manual method to flatten the data.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>SELECT
names ['first'] AS first_name,
names ['last'] AS last_name,
department
FROM
dataset
CROSS JOIN UNNEST(people) AS t (names)
</code>
<code>SELECT names ['first'] AS first_name, names ['last'] AS last_name, department FROM dataset CROSS JOIN UNNEST(people) AS t (names) </code>
SELECT
    names ['first'] AS first_name,
    names ['last'] AS last_name,
    department
FROM
    dataset
    CROSS JOIN UNNEST(people) AS t (names)

But data in these columns is huge (around 5000 columns in one column)
So, manual method of select is taking lot of time.

Is there any alternate faster way to flatten the json column into individual columns?

1

This is a limitation with SQL. All columns must be known before a query runs, dynamic generation of columns is not support in SQL (at least ANSI/standard). Wildcards like SELECT * are allowed, because the * can be expanded during query planning using the table schema.

I don’t know the schema of your table, which makes answering a bit tricky, but assuming you are querying JSON data you can declare the people column as a struct and include all the properties. This will give the query planner enough information for your queries to be able to wildcard expand all the property names.

For example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>CREATE EXTERNAL TABLE dataset (
department string,
people array<struct<first:string,last:string>>
)
</code>
<code>CREATE EXTERNAL TABLE dataset ( department string, people array<struct<first:string,last:string>> ) … </code>
CREATE EXTERNAL TABLE dataset (
  department string,
  people array<struct<first:string,last:string>>
)
…

With the table above, you can do this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>SELECT department, person.*
FROM dataset
CROSS JOIN UNNEST(people) AS t (person)
</code>
<code>SELECT department, person.* FROM dataset CROSS JOIN UNNEST(people) AS t (person) </code>
SELECT department, person.*
FROM dataset
CROSS JOIN UNNEST(people) AS t (person)

Here is a working example that uses a CTE to provide the data. row is the same thing as struct, you just need to use the latter in DDL and the former in DML – DDL uses Hive syntax and naming, DML uses Trino syntax and naming:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>with
example_data (people) as (values '[{"first":"f1","last":"l1"},{"first":"f2","last":"l2"}]',
'[{"first":"f3","last":"l3"}]'),
parsed_data as (select cast(json_parse(people) as array(row(first varchar, last varchar))) as people
from example_data)
select t.person.*
from parsed_data
cross join unnest(people) as t (person)
</code>
<code>with example_data (people) as (values '[{"first":"f1","last":"l1"},{"first":"f2","last":"l2"}]', '[{"first":"f3","last":"l3"}]'), parsed_data as (select cast(json_parse(people) as array(row(first varchar, last varchar))) as people from example_data) select t.person.* from parsed_data cross join unnest(people) as t (person) </code>
with
  example_data (people) as (values '[{"first":"f1","last":"l1"},{"first":"f2","last":"l2"}]',
                                   '[{"first":"f3","last":"l3"}]'),
  parsed_data as (select cast(json_parse(people) as array(row(first varchar, last varchar))) as people
                    from example_data)
select t.person.*
from parsed_data
cross join unnest(people) as t (person)

Now, will this be practical with 5000 columns? No. You will also run into other issues with that many columns. Anything above a couple of hundred columns risks running into internal limits. Query plans can’t be infinite in size, and the list of column names has a definite limit (which isn’t related to the number of columns, but the memory usage of the compiled form, which means that long names and larger types reaches the limit faster).

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật