Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

360
Views
How to convert recordset to json array in postgres

i'm having one table like

  name | age 
   abc | 20
   pqr | 30

I want result in json array like

{
 [{
   "name":"abc",
   "age":20 
 },
 {
  "name":"pqr",
   "age":30 
 }]
}

I know their is method

row_to_json();

that will be give me only single row of json but i want array, please help me on this

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

select json_agg(row_to_json(t))
from t
;
                      json_agg                      
----------------------------------------------------
 [{"name":"abc","age":20}, {"name":"pqr","age":30}]
over 4 years ago · Santiago Trujillo Report

0

You can try:

SELECT array_to_json(array_agg(row_to_json(data)))
FROM (select name, age from your_table) data

You also can see more at this link:

https://hashrocket.com/blog/posts/faster-json-generation-with-postgresql

Hope it useful to you.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!