I tried to group below response by name:
{
"document1": [
{
"row": {
"schema": "Schemas",
"view": "CustomerAddress",
"data": {
"rownum": "1",
"CustomerName": "Name1",
"AddrTypeCd": "MailingAddress",
"Addr1": "911 FORBES AVE",
"Addr2": "SUITE XXX",
"CityName": "asdfasfd",
"StateProvCd": "PA",
"PostalCode": "15219"
}
}
},
{
"row": {
"schema": "Schemas",
"view": "CustomerAddress",
"data": {
"rownum": "2",
"CustomerName": "Name1",
"AddrTypeCd": "OfficeAddress",
"Addr1": "911 Watson AVE",
"Addr2": "SUITE XXX",
"CityName": "asdfasfd",
"StateProvCd": "CT",
"PostalCode": "15119"
}
}
}
],
"document2": []
}
I used arrayAggregate
method in this way
.groupBy(customerAddreses.col('CustomerName'),
op.arrayAggregate("Customer addresses", customerAddreses.col('Addr1')))
and my current response data look like this:
{
"schemas.customerAddreses.CustomerName": "Name1",
"Customer addresses": [
"911 FORBES AVE",
"911 Watson AVE"
]
}
Is it possible to call arrayAggregate
with mutiltiple columns and receive object in Customer addresses array? Below is the response what I want to obtain:
{
"schemas.customerAddreses.CustomerName": "Name1",
"Customer addresses": [
{
"Addr1": "911 FORBES AVE",
"Addr2": "SUITE XXX",
"CityName": "asdfasfd",
"StateProvCd": "PA",
},
{
"Addr1": "911 Watson AVE",
"Addr2": "SUITE XXX",
"CityName": "asdfasfd",
"StateProvCd": "CT"
}
]
}
Input
{
"document1": [
{
"data": {
"rownum": "1",
"CustomerName": "Name1",
"AddrTypeCd": "MailingAddress",
"Addr1": "911 FORBES AVE",
"Addr2": "SUITE XXX",
"CityName": "asdfasfd",
"StateProvCd": "PA",
"PostalCode": "15219"
}
},
{
"data": {
"rownum": "2",
"CustomerName": "Name1",
"AddrTypeCd": "OfficeAddress",
"Addr1": "911 Watson AVE",
"Addr2": "SUITE XXX",
"CityName": "asdfasfd",
"StateProvCd": "CT",
"PostalCode": "15119"
}
}
]
}
JavaScript Optic
op.fromView('main', 'CustomerAddress')
.select(['Addr1',
'Addr2',
'CityName',
'StateProvCd',
'CustomerName',
op.as('addrAggr', op.jsonObject([
op.prop('Addr1', op.col('Addr1')),
op.prop('Addr2', op.col('Addr2')),
op.prop('CityName', op.col('CityName')),
op.prop('StateProvCd', op.col('StateProvCd'))
]))
])
.orderBy(op.asc('Addr1')) //Line14
.groupBy('CustomerName', op.arrayAggregate("Customer addresses", "addrAggr"))
.result();
Result
{
"CustomerName": "Name1",
"Customer addresses": [
{
"Addr1": "911 FORBES AVE",
"Addr2": "SUITE XXX",
"CityName": "asdfasfd",
"StateProvCd": "PA"
},
{
"Addr1": "911 Watson AVE",
"Addr2": "SUITE XXX",
"CityName": "asdfasfd",
"StateProvCd": "CT"
}
]
}
Note
CustomerAddress
while another customerAddreses
. Not sure how you could get any result.ORDER
, please add sort
clause (Line14
) in the Optic.address
instead of document1
.