Я пытаюсь сопоставить плоскую структуру с иерархической структурой, используя Jolt http://jolt -demo.appspot.com / # andrewkcarter2 :
Мой ввод - это плоская структура, содержащая информацию о клиенте: ниже мой ввод JSON:
[
{
"customerId": "100",
"customerName": "Ken",
"accountId": "1001",
"accountType": "SAV",
"transactionAmount": "100.00",
"homeaddress": "800 W Trade St",
"businessaddress": "440 S Church St"
},
{
"customerId": "100",
"customerName": "Ken",
"accountId": "1001",
"accountType": "SAV",
"transactionAmount": "15.00",
"homeaddress": "800 W Trade St",
"businessaddress": "440 S Church St"
},
{
"customerId": "100",
"customerName": "Ken",
"accountId": "1002",
"accountType": "CHK",
"transactionAmount": "200.00",
"homeaddress": "900 E 4th St",
"businessaddress": "500 N Church St"
},
{
"customerId": "100",
"customerName": "Ken",
"accountId": "1002",
"accountType": "CHK",
"transactionAmount": "116.00",
"homeaddress": "900 E th St",
"businessaddress": "500 N Church St"
}
]
Вот мой JOLT Spec:
[
{
"operation": "shift",
"spec": {
"*": {
"customerId": {
"*": {
"@2": "temp.&1[]"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"temp": {
"*": {
"0": {
"customerId": "[#3].customerId",
"customerName": "[#3].customerName",
"accountId": "[#3].accounts[0].accountId",
"accountType": "[#3].accounts[0].accountType",
"transactionAmount": "
[#3].accounts[0].transactions[0].transactionAmount",
"homeaddress": "[#3].addresses[0].homeaddress",
"businessaddress": "[#3].addresses[0].businessaddres"
},
"*": {
"accountId": "[#3].accounts[&1].accountId",
"accountType": "[#3].accounts[&1].accountType",
"transactionAmount": "[#3].accounts[&1].transactions[&1].&",
"homeaddress": "[#3].addresses[&1].homeaddress",
"businessaddress": "[#3].addresses[&1].businessaddress"
}
}
}
}
}
]
Результат, который я получаю:
[ {
"customerId" : "100",
"customerName" : "Ken",
"accounts" : [ {
"accountId" : "1001",
"accountType" : "SAV",
"transactions" : [ {
"transactionAmount" : "100.00"
} ]
}, {
"accountId" : "1001",
"accountType" : "SAV",
"transactions" : [ null, {
"transactionAmount" : "15.00"
} ]
}, {
"accountId" : "1002",
"accountType" : "CHK",
"transactions" : [ null, null, {
"transactionAmount" : "200.00"
} ]
}, {
"accountId" : "1002",
"accountType" : "CHK",
"transactions" : [ null, null, null, {
"transactionAmount" : "116.00"
} ]
} ],
"addresses" : [ {
"homeaddress" : "800 W Trade St",
"businessaddres" : "440 S Church St"
}, {
"homeaddress" : "800 W Trade St",
"businessaddress" : "440 S Church St"
}, {
"homeaddress" : "900 E 4th St",
"businessaddress" : "500 N Church St"
}, {
"homeaddress" : "900 E th St",
"businessaddress" : "500 N Church St"
} ]
} ]
В приведенном выше выводе мы видим, что счета 1001 и 1002 повторяются столько раз, сколько транзакций. Я не хочу, чтобы это произошло, и я хочу, чтобы транзакции были сгруппированы по соответствующим учетным записям.
Итак, мой желаемый результат должен быть:
[ {
"customerId" : "100",
"customerName" : "Ken",
"accounts" : [ {
"accountId" : "1001",
"accountType" : "SAV",
"transactions" : [ {
"transactionAmount" : "100.00"
},
{
"transactionAmount" : "15.00"
} ]
}, {
"accountId" : "1002",
"accountType" : "CHK",
"transactions" : [ null, null, {
"transactionAmount" : "200.00"
},{
"transactionAmount" : "116.00"
} ]
},
"addresses" : [ {
"homeaddress" : "800 W Trade St",
"businessaddres" : "440 S Church St"
}, {
"homeaddress" : "900 E th St",
"businessaddress" : "500 N Church St"
} ]
} ]
Аналогичным образом в результате должны быть указаны только уникальные адреса.
Есть ли способ сгруппировать транзакции по соответствующим счетам. Может ли кто-нибудь помочь с этим?