简体   繁体   English

SNOWFLAKE - 将整个表转换为 JSON?

[英]SNOWFLAKE-Convert an entire table to JSON?

Convert an entire table to JSON?将整个表转换为 JSON?

I have the following table [table1]:我有下表 [table1]:

在此处输入图像描述

I would like to convert all this into a new table and a single column with JSON format, is it possible?我想将所有这些转换成一个新表和一个格式为 JSON 的单列,这可能吗?

Desired result new table [table2]:期望的结果新表 [table2]:

[ table] | [表] | NewColumnJSON|新列JSON|

{
  "[1]": {
    "Column B ": " hello 1 ",
    "Column C ": " world1",
  },
  "[2]": {
    "Column B ": " hello 2 ",
    "Column C ": " world2",
  },
  "[3]": {
    "Column B ": " hello 3 ",
    "Column C ": " world3",
  },
}

Thank you for your help.谢谢您的帮助。

With this great CTE to act as our Table:有了这个伟大的 CTE 作为我们的表:

WITH fake_data(columnA, columnB, columnC) as (
    select * from values
    (1, 'hello1', 'world1'),
    (2, 'hello2', 'world2'),
    (3, 'hello3', 'world3')
)

we can use this SQL:我们可以使用这个 SQL:

SELECT columnA, object_construct('column b', columnb, 'column c', columnc) as obj
FROM fake_data;

then we can use OBJECT_CONSTRUCT to us the sub-objects:然后我们可以对我们的子对象使用OBJECT_CONSTRUCT

COLUMNA专栏 OBJ目标文件
1 1个 { "column b": "hello1", "column c": "world1" } { “b 列”:“hello1”,“c 列”:“world1” }
2 2个 { "column b": "hello2", "column c": "world2" } { “b 列”:“hello2”,“c 列”:“world2” }
3 3个 { "column b": "hello3", "column c": "world3" } { “b 列”:“hello3”,“c 列”:“world3” }

which we can wrap in OBJECT_AGG我们可以将其包装在OBJECT_AGG

SELECT object_agg(columnA, object_construct('column b', columnb, 'column c', columnc)) as obj
FROM fake_data;
OBJ目标文件
{ "1": { "column b": "hello1", "column c": "world1" }, "2": { "column b": "hello2", "column c": "world2" }, "3": { "column b": "hello3", "column c": "world3" } } {“1”:{“b 列”:“hello1”,“c 列”:“world1”},“2”:{“b 列”:“hello2”,“c 列”:“world2”},“ 3": { "b 列": "hello3", "c 列": "world3" } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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