Postgresql中的JSON输出 [英] JSON output in Postgresql

查看:205
本文介绍了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.

推荐答案

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输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆