SNOWFLAKE - 将整个 table 转换为 JSON?
SNOWFLAKE-Convert an entire table to JSON?
将整个 table 转换为 JSON?
我有以下 table [table1]:
我想将所有这些转换成新的 table 和 JSON 格式的单列,可以吗?
期望的结果新 table [table2] :
[table]
|新列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",
},
}
感谢您的帮助。
有了这个伟大的 CTE 作为我们的 Table:
WITH fake_data(columnA, columnB, columnC) as (
select * from values
(1, 'hello1', 'world1'),
(2, 'hello2', 'world2'),
(3, 'hello3', 'world3')
)
我们可以使用这个 SQL:
SELECT columnA, object_construct('column b', columnb, 'column c', columnc) as obj
FROM fake_data;
然后我们可以使用 OBJECT_CONSTRUCT 给我们 sub-objects:
COLUMNA
OBJ
1
{ "column b": "hello1", "column c": "world1" }
2
{ "column b": "hello2", "column c": "world2" }
3
{ "column b": "hello3", "column c": "world3" }
我们可以将其包裹在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" } }
将整个 table 转换为 JSON?
我有以下 table [table1]:
我想将所有这些转换成新的 table 和 JSON 格式的单列,可以吗?
期望的结果新 table [table2] :
[table] |新列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",
},
}
感谢您的帮助。
有了这个伟大的 CTE 作为我们的 Table:
WITH fake_data(columnA, columnB, columnC) as (
select * from values
(1, 'hello1', 'world1'),
(2, 'hello2', 'world2'),
(3, 'hello3', 'world3')
)
我们可以使用这个 SQL:
SELECT columnA, object_construct('column b', columnb, 'column c', columnc) as obj
FROM fake_data;
然后我们可以使用 OBJECT_CONSTRUCT 给我们 sub-objects:
COLUMNA | OBJ |
---|---|
1 | { "column b": "hello1", "column c": "world1" } |
2 | { "column b": "hello2", "column c": "world2" } |
3 | { "column b": "hello3", "column c": "world3" } |
我们可以将其包裹在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" } } |