简体   繁体   中英

Key value table to json in BigQuery

Hey all,

I have a table that looks like this:

row key val
1 a 100
2 b 200
3 c "apple
4 d {}

I want to convert it into JSON:

{
    "a": 100,
    "b": 200,
    "c": "apple",
    "d": {}
}

Note: the number of lines can change so this is only an example

Thx in advanced !

With string manipulation,

WITH sample_table AS (
  SELECT 'a' key, '100' value UNION ALL
  SELECT 'b', '200' UNION ALL
  SELECT 'c', '"apple"' UNION ALL
  SELECT 'd', '{}'
)
SELECT '{' || STRING_AGG(FORMAT('"%s": %s', key, value)) || '}' json
  FROM sample_table;

You can get following result similar to your expected output.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM