如何将雪花响应变成JSON格式?

发布于 2025-02-11 11:30:22 字数 397 浏览 0 评论 0原文

这是我正在开发的Spring Boot API的一部分。我正在查询雪花以使用这样的查询来获取一些数据:

select distinct OBJECT_CONSTRUCT(
'id', id,
'name', name,
'etc', etc
) as RESPONSE from ...

我获得了类似格式的字符串列表:

list:
0-> {'id':1, 'name':name, 'etc':etc}
1-> {'id':2, 'name':name, 'etc':etc}
2-> {'id':3, 'name':name, 'etc':etc}
...

我可以对API可以返回的单个JSONNODE响应获得此响应的最清洁方法?

This is part of a spring boot API I am developing. I am querying snowflake to get some data using a query like this:

select distinct OBJECT_CONSTRUCT(
'id', id,
'name', name,
'etc', etc
) as RESPONSE from ...

I am getting a a List of String which is formatted like this:

list:
0-> {'id':1, 'name':name, 'etc':etc}
1-> {'id':2, 'name':name, 'etc':etc}
2-> {'id':3, 'name':name, 'etc':etc}
...

What is the cleanest way that I can get this response to a single JsonNode response that the api can return?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月野兔 2025-02-18 11:30:22

原始语句,生产单个行 -

select distinct object_construct('response',OBJECT_CONSTRUCT(
'id', id,
'name', name,
'etc', etc
)) as RESPONSE from
(select 1 as id, 'name' as name, 'etc' as etc union all
select 2, 'name','etc' union all
select 3, 'name','etc');
+--------------------+
| RESPONSE           |
|--------------------|
| {                  |
|   "response": {    |
|     "etc": "etc",  |
|     "id": 1,       |
|     "name": "name" |
|   }                |
| }                  |
| {                  |
|   "response": {    |
|     "etc": "etc",  |
|     "id": 2,       |
|     "name": "name" |
|   }                |
| }                  |
| {                  |
|   "response": {    |
|     "etc": "etc",  |
|     "id": 3,       |
|     "name": "name" |
|   }                |
| }                  |
+--------------------+
3 Row(s) produced. 

接下来,我们将使用array_agg将结果粘合到单个实体中。

select distinct array_agg(OBJECT_construct(
'id', id,
'name', name,
'etc', etc
)) as RESPONSE from
(select 1 as id, 'name' as name, 'etc' as etc union all
select 2, 'name','etc' union all
select 3, 'name','etc');
+--------------------+
| RESPONSE           |
|--------------------|
| [                  |
|   {                |
|     "etc": "etc",  |
|     "id": 1,       |
|     "name": "name" |
|   },               |
|   {                |
|     "etc": "etc",  |
|     "id": 2,       |
|     "name": "name" |
|   },               |
|   {                |
|     "etc": "etc",  |
|     "id": 3,       |
|     "name": "name" |
|   }                |
| ]                  |
+--------------------+
1 Row(s) produced. 

如果需要,可以根据需要通过to_jsonparse_json进一步转换结果。

以下将提供字符串输出 -

select distinct to_json(array_agg(OBJECT_construct(
'id', id,
'name', name,
'etc', etc
))) as RESPONSE from
(select 1 as id, 'name' as name, 'etc' as etc union all
select 2, 'name','etc' union all
select 3, 'name','etc');
+------------------------------------------------------------------------------------------------------------+
| RESPONSE                                                                                                   |
|------------------------------------------------------------------------------------------------------------|
| [{"etc":"etc","id":1,"name":"name"},{"etc":"etc","id":2,"name":"name"},{"etc":"etc","id":3,"name":"name"}] |
+------------------------------------------------------------------------------------------------------------+
1 Row(s) produced.

以下将提供JSON输出 -

select distinct parse_json(to_json(array_agg(OBJECT_construct(
'id', id,
'name', name,
'etc', etc
)))) as RESPONSE from
(select 1 as id, 'name' as name, 'etc' as etc union all
select 2, 'name','etc' union all
select 3, 'name','etc');

+--------------------+
| RESPONSE           |
|--------------------|
| [                  |
|   {                |
|     "etc": "etc",  |
|     "id": 1,       |
|     "name": "name" |
|   },               |
|   {                |
|     "etc": "etc",  |
|     "id": 2,       |
|     "name": "name" |
|   },               |
|   {                |
|     "etc": "etc",  |
|     "id": 3,       |
|     "name": "name" |
|   }                |
| ]                  |
+--------------------+
1 Row(s) produced.

Original statement, producing individual rows -

select distinct object_construct('response',OBJECT_CONSTRUCT(
'id', id,
'name', name,
'etc', etc
)) as RESPONSE from
(select 1 as id, 'name' as name, 'etc' as etc union all
select 2, 'name','etc' union all
select 3, 'name','etc');
+--------------------+
| RESPONSE           |
|--------------------|
| {                  |
|   "response": {    |
|     "etc": "etc",  |
|     "id": 1,       |
|     "name": "name" |
|   }                |
| }                  |
| {                  |
|   "response": {    |
|     "etc": "etc",  |
|     "id": 2,       |
|     "name": "name" |
|   }                |
| }                  |
| {                  |
|   "response": {    |
|     "etc": "etc",  |
|     "id": 3,       |
|     "name": "name" |
|   }                |
| }                  |
+--------------------+
3 Row(s) produced. 

Next we will use ARRAY_AGG, to get result clubbed into single entity.

select distinct array_agg(OBJECT_construct(
'id', id,
'name', name,
'etc', etc
)) as RESPONSE from
(select 1 as id, 'name' as name, 'etc' as etc union all
select 2, 'name','etc' union all
select 3, 'name','etc');
+--------------------+
| RESPONSE           |
|--------------------|
| [                  |
|   {                |
|     "etc": "etc",  |
|     "id": 1,       |
|     "name": "name" |
|   },               |
|   {                |
|     "etc": "etc",  |
|     "id": 2,       |
|     "name": "name" |
|   },               |
|   {                |
|     "etc": "etc",  |
|     "id": 3,       |
|     "name": "name" |
|   }                |
| ]                  |
+--------------------+
1 Row(s) produced. 

If required, the result can be further converted as needed via TO_JSON or PARSE_JSON.

Below will give string output -

select distinct to_json(array_agg(OBJECT_construct(
'id', id,
'name', name,
'etc', etc
))) as RESPONSE from
(select 1 as id, 'name' as name, 'etc' as etc union all
select 2, 'name','etc' union all
select 3, 'name','etc');
+------------------------------------------------------------------------------------------------------------+
| RESPONSE                                                                                                   |
|------------------------------------------------------------------------------------------------------------|
| [{"etc":"etc","id":1,"name":"name"},{"etc":"etc","id":2,"name":"name"},{"etc":"etc","id":3,"name":"name"}] |
+------------------------------------------------------------------------------------------------------------+
1 Row(s) produced.

Following will give JSON output -

select distinct parse_json(to_json(array_agg(OBJECT_construct(
'id', id,
'name', name,
'etc', etc
)))) as RESPONSE from
(select 1 as id, 'name' as name, 'etc' as etc union all
select 2, 'name','etc' union all
select 3, 'name','etc');

+--------------------+
| RESPONSE           |
|--------------------|
| [                  |
|   {                |
|     "etc": "etc",  |
|     "id": 1,       |
|     "name": "name" |
|   },               |
|   {                |
|     "etc": "etc",  |
|     "id": 2,       |
|     "name": "name" |
|   },               |
|   {                |
|     "etc": "etc",  |
|     "id": 3,       |
|     "name": "name" |
|   }                |
| ]                  |
+--------------------+
1 Row(s) produced.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文