There are multiple items in the array in which I am trying to remove the item whose idTypDesc is id if there is another item in array with idTypDesc as Ultimate ID. if the array doesn’t have item with idTypDesc as Ultimate ID then don’t remove the item whose idTypDesc is id
Input:
{
"PartyAlternateId": {
"item": [
{
"AltIdType": {
"idTypDesc": "id",
"idTyp": "0004"
}
},
{
"AltIdVal": "hjn",
"AltIdType": {
"idTypDesc": "ultra ID",
"idTyp": "0018"
}
},
{
"AltIdType": {
"idTypDesc": "Ultimate ID",
"idTyp": "0018"
}
}
]
}
}
Expected Output:
{
"PartyAlternateId": {
"item": [
{
"AltIdVal": "hjn",
"AltIdType": {
"idTypDesc": "ultra ID",
"idTyp": "0018"
}
},
{
"AltIdType": {
"idTypDesc": "Ultimate ID",
"idTyp": "0018"
}
}
]
}
}
2
Answers
You can optionally match through use of
"":""
key-value pair within a shift ransforation spec such asthe demo on the site https://jolt-demo.appspot.com/ is :
It seems like the spec you provided only meets the condition when the Ultra id is present but it wont keep the id object if its not present. Can I suggest the following spec:
Even though the spec appears to be long but its really simple to follow:
1- Bucket all id objects into IdsBucket parent object and assign each id object priority (the lower the number the higher in priority)
2- Apply sort operation to bring item with highest priority (lowest number) to the top.
3- Make IdsBucket an array. Unfortunately the sort above wont work if its already an array.
4- select the first element (index 0) from the IdsBucket array above and move it back to the item array.
If the sort happen to work on an array of complex objects, the 3rd spec wont be needed.
Let me know what you think.