简体   繁体   中英

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:

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. I reduce the number of columns in the query and it returns a proper json object. Does anyone have any idea about this? Thanks

I also tried the query after omitting json_agg but still the same result

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:

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

Having tens or hundreds of args is not nice anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM