What you are describing is often referred to as a pivot table - a transformation where values are used as columns. SQL doesn't generally support this as SQL is designed around the concept of having a fixed schema while pivot table requires dynamic schemas.

However if you have a fixed set of index columns you can emulate it with something like:

SELECT
  name,
  age,
  ARRAY(SELECT value FROM UNNEST(customFields) WHERE index="1")[SAFE_OFFSET(0)] AS index_1,
  ARRAY(SELECT value FROM UNNEST(customFields) WHERE index="2")[SAFE_OFFSET(0)] AS index_2,
  ARRAY(SELECT value FROM UNNEST(customFields) WHERE index="3")[SAFE_OFFSET(0)] AS index_3
FROM your_table;

What this does is specifically define columns for each index that picks out the right values from the customFields array.

Answer from David on Stack Overflow
🌐
Google Cloud
cloud.google.com › bigquery › work with arrays
Work with arrays | BigQuery | Google Cloud Documentation
Because UNNEST destroys the order of the ARRAY elements, you may wish to restore order to the table. To do so, use the optional WITH OFFSET clause to return an additional column ...
🌐
Medium
medium.com › data-engineers-notes › unnesting-arrays-in-bigquery-c1b48d413ece
UNNESTING ARRAYS in BigQuery
April 15, 2024 - Working with them is quite straightforward once you get the hang of it. Probably the most common operation to do with them is to UNNEST them. The ARRAY is basically a set of rows inside one of the columns.
Discussions

Is it possible to UNNEST an array in BigQuery so that the nested data in split into columns by a key value? - Stack Overflow
I've only been able to unnest this data so that the "index" and "value" fields are columns: More on stackoverflow.com
🌐 stackoverflow.com
sql - How to unnest BigQuery nested records into multiple columns - Stack Overflow
I am trying to unnest the below table . Using the below unnest query to flatten the table SELECT id, name ,keyword FROM `project_id.dataset_id.table_id` ,unnest (`groups` ) as `groups` where id = More on stackoverflow.com
🌐 stackoverflow.com
sql - Unnest array on bigQuery - Stack Overflow
My BigQuery table has some regular columns plus one array of structs. I'd like to run a query to flatten this structure, having one row for each element of the array, and duplicated values for the ... More on stackoverflow.com
🌐 stackoverflow.com
google bigquery - How to flatten an array with UNNEST or any other functions? - Stack Overflow
Suppose I get a temporary table with one field is an array, how to turn it to multiple rows ? With PostgreSQL this can be done with UNNEST http://sqlfiddle.com/#!15/21673/19 WITH x AS (SELECT ARR... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Count
count.co › sql-resources › bigquery-standard-sql › unnest
UNNEST | BigQuery Standard SQL - Count.co
This will create multiple rows for each element of your array but you can then filter it down. SELECT * FROM UNNEST([1, 2, 2, 5, NULL]) AS unnest_column
🌐
Learn BigQuery!
scosco.github.io › learn-bigquery › foundation › subqueries.html
Subqueries: Arrays + UNNEST() | Learn BigQuery!
Using Structs means to have a fixed ... Every struct provides the columns of their respective row. To sum up: UNNEST() turns array elements into rows and struct elements (within arrays) into columns....
🌐
Yuichi Otsuka
yuichiotsuka.com › home › blog › bigquery unnest and working with arrays
BigQuery UNNEST and Working with Arrays - Yuichi Otsuka
June 10, 2024 - For example, if you attempt to ... an array column, you will get an error as follows: ORDER BY does not support expressions of type ARRAY<…> at … · You are also not allowed to use an array as a JOIN condition, getting an error similar to the following message: Equality is not defined for arguments of type ARRAY<…> at … · We need to use the BigQuery UNNEST function to flatten an array into its ...
🌐
MarquinSmith
marquinsmith.com › home › data analysis › unnesting in bigquery – a guided introduction
UNNESTing in BigQuery – a guided introduction | MarquinSmith
October 5, 2023 - You can use UNNEST to explode the array into separate rows: SELECT order_id, item FROM orders, UNNEST(items) AS item ... You can also combine UNNEST with other functions to perform more complex operations.
Find elsewhere
🌐
Sheetgo
sheetgo.com › home › how to use unnest in bigquery
How to use unnest in BigQuery - Sheetgo
December 17, 2025 - The UNNEST function in BigQuery is used to flatten nested or repeated data structures into separate rows. What it does is take as input a column with a nested data type like an ARRAY, and expand the nested or repeated elements into multiple rows.
🌐
DiveTeam
ken-williams.com › home › blog › how to flatten a bigquery table with unnest
How to Flatten a BigQuery Table with UNNEST - Ken Williams
May 10, 2020 - As an example, let’s say that ... UNNEST operator Works UNNEST allows you to flatten the “event_params” column so that each item in the array creates a single row in the table with two new columns: “event_params.key” ...
Top answer
1 of 4
6

That's because the comma is a cross join - in combination with an unnested array it is a lateral cross join. You repeat the parent row for every row in the array.

One problem with pivoting arrays is that arrays can have a variable amount of rows, but a table must have a fixed amount of columns.

So you need a way to decide for a certain row that becomes a certain column.

E.g. with

SELECT 
  id,
  name,
  groups[ordinal(1)] as firstArrayEntry,
  groups[ordinal(2)] as secondArrayEntry,
  keyword
FROM `project_id.dataset_id.table_id`
unnest(groups)
where id = 204358

If your array had a key-value pair you could decide using the key. E.g.

SELECT 
  id,
  name,
  (select value from unnest(groups) where key='key1') as key1,
  keyword
FROM `project_id.dataset_id.table_id`
unnest(groups)
where id = 204358

But that doesn't seem to be the case with your table ...

A third option could be PIVOT in combination with your cross-join solution but this one has restrictions too: and I'm not sure how computation-heavy this is.

2 of 4
2

Consider below simple solution

select * from (
  select id, name, keyword, offset
  from `project_id.dataset_id.table_id`,
  unnest(`groups`) with offset 
) pivot (max(name) name for offset + 1 in (1, 2))      

if applied to sample data in your question - output is

Note , when you apply to your real case - you just need to know how many such name_NNN columns to expect and extend respectively list - for example for offset + 1 in (1, 2, 3, 4, 5)) if you expect 5 such columns

In case if for whatever reason you want improve this - use below where everything is built dynamically for you so you don't need to know in advance how many columns it will be in the output

execute immediate (select '''
select * from (
  select id, name, keyword, offset
  from `project_id.dataset_id.table_id`,
  unnest(`groups`) with offset 
) pivot (max(name) name for offset + 1 in (''' ||  string_agg('' || pos, ', ')  || '''))
'''
from (select pos from (
  select max(array_length(`groups`)) cnt
  from `project_id.dataset_id.table_id` 
  ), unnest(generate_array(1, cnt)) pos 
))
🌐
Learn BigQuery!
scosco.github.io › learn-bigquery › foundation › flatten.html
Flatten Nested Tables | Learn BigQuery!
When working with BigQuery data it might happen quite often that you want to group by a field that sits inside of an array. The problem is that you can’t group arrays. We somehow have to get these fields out of the array! Let’s tackle this problem! Remember how UNNEST() is turning arrays into table rows and structs into table columns...
🌐
Medium
paddyalton.medium.com › google-bigquery-how-to-unwrap-an-array-like-field-into-a-wide-format-table-with-one-column-per-14c0686b4fc7
Google BigQuery — how to unwrap an array-like field into a wide-format table with one column per array element | by Paddy Alton | Medium
April 16, 2022 - The first thing we want to do is get the values out of the arrays and into rows. Typically in BigQuery this is accomplished using CROSS JOIN. The syntax is a tad unintuitive: WITH raw AS ( SELECT "A" AS name, [1,2,3,4,5] AS a UNION ALL SELECT "B" AS name, [5,4,3,2,1] AS a ), long_format AS ( SELECT name, vals FROM raw CROSS JOIN UNNEST(raw.a) AS vals )SELECT * FROM long_format
🌐
OneUptime
oneuptime.com › home › blog › how to flatten nested and repeated fields in bigquery with unnest
How to Flatten Nested and Repeated Fields in BigQuery with UNNEST
February 17, 2026 - -- Create a sample table with an array column CREATE TEMP TABLE products AS SELECT 'P001' AS product_id, 'Laptop' AS name, ['electronics', 'computers', 'portable'] AS tags UNION ALL SELECT 'P002', 'Headphones', ['electronics', 'audio', 'accessories'] UNION ALL SELECT 'P003', 'Desk', ['furniture', 'office']; -- Flatten the tags array into individual rows SELECT product_id, name, tag FROM products, UNNEST(tags) AS tag; -- Returns: -- P001 Laptop electronics -- P001 Laptop computers -- P001 Laptop portable -- P002 Headphones electronics -- P002 Headphones audio -- P002 Headphones accessories -- P003 Desk furniture -- P003 Desk office · The more common case is arrays of structured objects. This is how nested data typically appears in BigQuery.
🌐
Towards Data Science
towardsdatascience.com › home › latest › bigquery unnest: how to work with nested data in bigquery
BigQuery UNNEST: How to work with nested data in BigQuery | Towards Data Science
January 24, 2025 - Again, we use the BigQuery UNNEST function to achieve this. The basic approach is to: ... Reconstruct the structure using the ARRAY and STRUCT datatypes accordingly.
🌐
Reddit
reddit.com › r/bigquery › bigquery unnest and working with arrays
r/bigquery on Reddit: BigQuery UNNEST and Working with Arrays
June 10, 2020 - If the two array columns are related by the order in each array, you can look at the WITH OFFSET keyword for UNNEST. More replies ... There are work arounds that let you use different data types for each column in an array, and ways to nest ...