Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert stringified array into array in BigQuery?

Tags:

It so happens I have a stringified array in a field in BigQuery

'["a","b","c"]'

and I want to convert it to an array that BigQuery understands. I want to be able to do this in standard SQL:

with k as (select '["a","b","c"]' as x)
select unnested_x from k, unnest(x) unnested_x

I have tried JSON_EXTRACT('["a","b","c"]','$') and everything else I could find online.

Any ideas?

like image 518
BorHna Avatar asked Sep 13 '17 14:09

BorHna


People also ask

How do I flatten an array in BigQuery?

To flatten an entire column of ARRAY s while preserving the values of the other columns in each row, use a correlated cross join to join the table containing the ARRAY column to the UNNEST output of that ARRAY column.

Which function can be used in BigQuery to convert subquery result to array?

The ARRAY function returns an ARRAY with one element for each row in a subquery.

How do you use structs in BigQuery?

What are Structs and how are they used in BigQuery: A struct is a data type that has attributes in key-value pairs, just like a dictionary in Python. Within each record, multiple attributes have their own values. These attributes can either be referred to as keys or Struct columns.


2 Answers

Below is for BigQuery Standard SQL

#standardSQL
WITH k AS (
  SELECT 1 AS id, '["a","b","c"]' AS x UNION ALL
  SELECT 2, '["x","y"]' 
)
SELECT 
  id, 
  ARRAY(SELECT * FROM UNNEST(SPLIT(SUBSTR(x, 2 , LENGTH(x) - 2)))) AS x
FROM k

It transforms your string column into array column

like image 189
Mikhail Berlyant Avatar answered Sep 20 '22 09:09

Mikhail Berlyant


Recently (2020) the JSON_EXTRACT_ARRAY function was added to the bigquery standard sql.

It makes it easy to get the expected behavior with no UDF or tricks

with k as (select JSON_EXTRACT_ARRAY('["a","b","c"]', '$') as x)
select unnested_x from k, unnest(x) unnested_x

Will result in:

╔══════════════╗
║ "unnested_x" ║
╠══════════════╣
║     "a"      ║
║     "b"      ║
║     "c"      ║
╚══════════════╝

JSON_EXTRACT_ARRAY doc

like image 28
Alonme Avatar answered Sep 21 '22 09:09

Alonme