Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an easy way to convert rows in BigQuery to JSON?

I am trying to reduce my rows in my BigQuery Table to JSON string. Is there a quick way to do this in standard SQL?

I thought about calling js with User-defined Function, but it forces me to declare the input type of the UDF so I can't generalize this function.

like image 863
dorachan2010 Avatar asked Jan 11 '17 17:01

dorachan2010


2 Answers

This is now possible using TO_JSON_STRING, which can also help when you want to pass a row from your table to a JavaScript UDF.

#standardSQL
WITH MyTable AS (
  SELECT 1 AS x, 'foo' AS y, true AS z UNION ALL
  SELECT 2, 'bar', false
)
SELECT TO_JSON_STRING(t) AS json
FROM MyTable AS t;
like image 121
Elliott Brossard Avatar answered Sep 27 '22 01:09

Elliott Brossard


If you want to glue together all of the rows quickly into a JSON block, you can do something like:

SELECT CONCAT("[", STRING_AGG(TO_JSON_STRING(t), ","), "]")
FROM `project.dataset.table` t

This will produce a table with 1 row that contains a complete JSON blob summarizing the entire table.

like image 3
bamnet Avatar answered Sep 28 '22 01:09

bamnet