The possible values for applicableOn field are already known but they are not limited to clothes and bags like given in this sample input. Also, there can be multiple elements in purchases array with the same applicableOn type and the sum of all those values should be calculated together.
Input:
{
"primary": {
"domainAttributes": {
"email": "[email protected]",
"name": "rach",
"age": "28"
},
"meta": {
"isEntity": true
},
"type": "somevalue",
"purchases": [
{
"domainAttributes": {
"email": "[email protected]",
"id": "1",
"age": "28"
},
"type": "somevalue",
"purchaseValue": [
{
"domainAttributes": {
"email": "[email protected]",
"name": "rach",
"id": "1",
"value": "5",
"applicableOn": "CLOTHES"
},
"meta": null,
"type": "some other value"
}
]
},
{
"domainAttributes": {
"email": "[email protected]",
"taxRateEntityId": "2",
"age": "28"
},
"type": "somevalue",
"purchaseValue": [
{
"domainAttributes": {
"email": "[email protected]",
"name": "rach",
"id": "2",
"value": "7",
"applicableOn": "BAGS"
},
"type": "some other value"
}
]
}
]
}
}
Expected output:
{
"email" : "[email protected]",
"name" : "rach",
"age" : "28",
"purchaseSum" : [{"sum":"5", "applicableOn": "CLOTHES"}, {"sum": "7", "applicableOn": "BAGS"}],
"purchase" : [ {
"value" : "5",
"id" : "1",
"applicableOn" : "CLOTHES"
}, {
"value" : "7",
"id" : "2",
"applicableOn" : "BAGS"
} ]
}
I have this spec currently but it calculates the sum of all values in purchaseValues without checking applicableType and also the format of purchase is slightly different than expected:
[
{
"operation": "shift",
"spec": {
"primary": {
"domainAttributes": {
"email": "email",
"name": "name",
"age": "age"
},
"purchases": {
"*": {
"purchaseValue": {
"*": {
"domainAttributes": {
"value": "purchases[].value",
"id": "purchases[].id",
"applicableOn": "purchases[].applicableOn"
}
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"email": "email",
"name": "name",
"age": "age",
"purchases": {
"*": {
"value": "purchaseSum[]",
"@": "purchase[]"
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"purchaseSum": "=intSum(@(1,purchaseSum))"
}
},
{
"operation": "shift",
"spec": {
"email": "email",
"name": "name",
"age": "age",
"purchaseSum": "purchaseSum",
"purchase": "purchase"
}
}
]
The current output I’m seeing with the above spec is:
{
"email" : "[email protected]",
"name" : "rach",
"age" : "28",
"purchaseSum" : 12,
"purchase" : [ {
"value" : "5"
}, {
"id" : "1"
}, {
"applicableOn" : "CLOTHES"
}, {
"value" : "7"
}, {
"id" : "2"
}, {
"applicableOn" : "BAGS"
} ]
}
2
Answers
The below spec should be dynamic enough to allow any type of applicableOn and any number of items per purchases.
Also since this looks more like querying the data than re structuring it, you can keep it short and sweet using JSLT: