How to convert stringified array into array in BigQuery?

BorHna picture BorHna · Sep 13, 2017 · Viewed 11.6k times · Source

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?

Answer

Mikhail Berlyant picture Mikhail Berlyant · Sep 13, 2017

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