以JSON格式数据到PostgreSQL中的表格

发布于 2025-02-13 21:17:17 字数 448 浏览 0 评论 0原文

在PostgreSQL中需要帮助JSON的帮助。我想要此查询

从面板中选择数据

返回:

                              Data
1 [{"type": "opened","user": "1"},{"type": "added","user":"2"}]
2 [{"type": "added","user": "3"}]
3 [{"type": "opened","user": "3"},{"type": "opened","user":"2"}]

返回表:

type    user
opened  1
added   2
added   3
opened  3
opened  2

我不太了解如何表示[和{和{{。我将感谢任何指导,因为我还没有找到这个特定的例子

Need help with json deserialization in postgresql. I would like this query

select data from panel

that returns:

                              Data
1 [{"type": "opened","user": "1"},{"type": "added","user":"2"}]
2 [{"type": "added","user": "3"}]
3 [{"type": "opened","user": "3"},{"type": "opened","user":"2"}]

instead returned a table:

type    user
opened  1
added   2
added   3
opened  3
opened  2

I don't quite understand how to represent the empty space between [ and {. I would appreciate any guidance as I have not found this particular example

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

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

发布评论

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

评论(1

你是暖光i 2025-02-20 21:17:17

使用JSONB_ARRAY_ELEMENTS将阵列转换为行,然后提取键:

select a.item ->> 'type' as type, 
       a.item ->> 'user' as "user"
from panel
  cross join jsonb_array_elements(data) as a(item);       

假设使用数据类型JSONB(是什么)假设data是定义的应该是)。如果不是,则必须施放它:data :: JSONB

Use jsonb_array_elements to turn the arrays in to rows, then extract the keys:

select a.item ->> 'type' as type, 
       a.item ->> 'user' as "user"
from panel
  cross join jsonb_array_elements(data) as a(item);       

This assumes that data is defined with the data type jsonb (which it should be). If it's not, you have to cast it: data::jsonb

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文