For my problem, I'm using json_agg in join to aggregate my result.
But this results in a nested array sequence.
Query:
SELECT c.*,
json_agg(ci.national_id) AS national_id,
json_agg(a.address) AS address
FROM company AS c
LEFT JOIN
(SELECT company_id,
json_agg(json_build_object('value', national_id, 'country', country_code)) AS national_id
FROM company_identification
GROUP BY company_id) AS ci ON ci.company_id = c.id
LEFT JOIN
(SELECT company_id,
json_agg(address.*) AS address
FROM address
GROUP BY company_id) AS a ON a.company_id = c.id
GROUP BY c.id
Result:
[
{
"id": "c876967d-dd8b-4068-88f4-57a438a2015c",
"name": "S.A.1",
"nationalId": [
[
{
"value": "string",
"country": "CL"
}
]
],
"address": [
[
{
"id": "d1362084-e652-4900-ba51-86352b7a8ce5",
"streetName": "First Avenue"
},
{
"id": "0f785a23-6eb3-44ea-9254-34a6f47ff638",
"streetName": "Second Avenue"
}
]
]
},
{
"id": "38557302-a6a3-4484-ae1b-27edc8c4e906",
"name": "S.A.",
"nationalId": [
[
{
"value": "Chile",
"country": "CL"
},
{
"value": "Colombia",
"country": "CO"
},
{
"value": "Mexico",
"country": "MX"
}
]
],
"address": [
[
{
"id": "d1362084-e652-4900-ba51-86352b7a8ce5",
"streetName": "First Avenue"
},
{
"id": "0f785a23-6eb3-44ea-9254-34a6f47ff638",
"streetName": "Second Avenue"
}
]
]
}
]
As you can see the nationalId and address fileds contains nested arrays.
sqlfiddle: http://www.sqlfiddle.com/#!17/9fde6/2
Output from sqlfiddle:
| id | name | national_id |
|----|-------|-----------------------------------------------|
| 1 | S.A.1 | [[{"value" : "Chile", "country" : "CL "}]] |
| 2 | S.A. | [[{"value" : "Colombia", "country" : "CO "}]] |
The national_id field should not contain array within array
[[ ... ]]
Desired Output:
| id | name | national_id |
|----|-------|-----------------------------------------------|
| 1 | S.A.1 | [{"value" : "Chile", "country" : "CL "}] |
| 2 | S.A. | [{"value" : "Colombia", "country" : "CO "}] |
JSON_AGGfunction you're using will always return values as an array. Can you update your question with some sample rows of how you want to see your data returned? Also, if you can post a SQL Fiddle with theDDLand sample data to play with that would make it easier for us to help you out. Here's one I started that you can modify - sqlfiddle.com/#!17/22202JSON_AGGtwice it results in[[ .... ]]. I want the output as[...]