Postgresql中的JSON输出

编程入门 行业动态 更新时间:2024-10-23 21:36:18
本文介绍了Postgresql中的JSON输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望我不会在这里遗漏一些很明显的东西, 我想从postgres函数获取JSON输出(我想很多其他人都已经需要这个了),我很乐意在我的服务器上安装contrib函数的扩展,

I hope I am not missing something very obvious here, I want to get JSON output from a postgres function (I imagine many other had already needed this) and I'd be happy to install an extension of contrib functions on my server,

有什么方法可以从sql或plpgsql函数(或在db-server-side python的帮助下)获取JSON输出吗?具体来说,我想将record[]结果作为JSON获取.

Is there any way to get JSON output from sql or plpgsql functions (or with help of db-server-side python)? Specifically I want to get my record[] results as JSON.

推荐答案

自 PostgreSQL 9.2 ,它在最新版本中具有许多其他功能(例如: PostgreSQL 0.4中的JSON函数).

There is built-in support for JSON since PostgreSQL 9.2 and it has increased with many other features in more recent versions(For example: JSON functions in PostgreSQL 0.4).

特别地,row_to_json将记录转换为JSON对象,而array_to_json将数组转换为JSON数组.

Specially the row_to_json converts a record into a JSON object and the array_to_json turns arrays into JSON arrays.

例如,可以将这两个函数结合起来以轻松地将SELECT查询的结果转换为JSON:

For example, both functions can be combined to easily turn the results of a SELECT query into JSON:

SELECT array_to_json(array_agg(row_to_json(t))) FROM (SELECT col1, col2, col3 FROM example_table) t

更多推荐

Postgresql中的JSON输出

本文发布于:2023-10-24 14:45:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524213.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Postgresql   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!