简体   繁体   English

无法使用 json_build_object 从 postgreSQL 查询创建大型 json 对象

[英]Unable to create a large json object from a postgreSQL query using json_build_object

I am trying to create a json object from my query using json_build_object as follows:我正在尝试使用 json_build_object 从我的查询中创建一个 json 对象,如下所示:

Select json_agg(json_build_object('first_name',first_name,
                                  'last_name',last_name,
                                  'email',email,
                                  'date_joined',date_joined,
                                  'verification_date',verification_date,
                                  'zip_code',zip_code,
                                  'city',city,
                                  'country',country)) 
from users 
WHERE last_name= 'xyz'

The json object builds fine with above shown number of columns however when i add all column names, the query gets stuck/hung indefinitely and no error message is displayed. json 对象使用上面显示的列数构建得很好,但是当我添加所有列名时,查询会无限期地卡住/挂起,并且不会显示任何错误消息。 I reduce the number of columns in the query and it returns a proper json object.我减少了查询中的列数,并返回一个正确的 json 对象。 Does anyone have any idea about this?有没有人对此有任何想法? Thanks谢谢

I also tried the query after omitting json_agg but still the same result在省略 json_agg 后我也尝试了查询,但结果仍然相同

I am not sure why your query hangs - could be that there is a limit on the number of args - but since you are building each JSON object in a trivial way (attribute names are the same as column names), try using row_to_json like this:我不确定为什么您的查询挂起 - 可能是 args 的数量有限制 - 但由于您正在以一种简单的方式构建每个 JSON 对象(属性名称与列名称相同),请尝试使用row_to_json像这样:

select json_agg(row_to_json(u.*)) from users u WHERE last_name = 'xyz';

Having tens or hundreds of args is not nice anyway.无论如何,拥有数十或数百个参数并不好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PostgreSQL json_build_object 嵌套 - PostgreSQL json_build_object nested 在PostgreSQL中使用json_build_object和json_build_array排序json结构 - Ordering json structure using json_build_object and json_build_array in Postgresql 如何为 json_build_object 添加默认值 postgresql - How to add a default value to json_build_object postgresql 在 PostgreSQL v14.x 中使用 json_build_object 和 SELECT 语句的结果 - Using json_build_object in PostgreSQL v14.x with the result of a SELECT statement json_build_object()中的大小写表达式 - Case expression in json_build_object() PostgreSQL Json_build_object / json_agg 添加聚合级别 - PostgreSQL Json_build_object / json_agg add level of aggregation 使用 json_agg(json_build_object(.......)) 时,如何确保 json_build_object 返回空列表 - when use json_agg(json_build_object(.......)) ,how to ensure json_build_object return empty list 如何在 Postgres 的 json_build_object() 中进行 GROUP BY 和 COUNT - How to do a GROUP BY and COUNT within json_build_object() in Postgres PostgresSQL 无法按 json_build_object 结果排序(从子查询获得) - PostgresSQL Cannot order by json_build_object result (got from subquery) 如何使用 ltree 查询结果创建分层 json 对象? (postgresql) - How to create hierarchal json object using ltree query results? (postgresql)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM