I have an array of 5 elements out of which I want to compare index position 1 and 2 and keep 1 or 2 based on weather it is an integer. Retain other elements as it is. Scenario below:
- if element at index position 1 is an integer and "" at position 2, keep element at position 1 in the array and remove element at index position 2.
- if element at index position 1 is "" and position 2 is an integer, keep that element in the array and remove position 1.
- if elements at position 1 and 2 are integers, then keep only the element at position 1 and remove element at position 2.
Final array should have only 4 elements
Input
{
"destination": [
{
"destinationName": ["dest1","dest2","dest3"],
"taxi_cost": [10,20,30],
"van_one_way_cost": [7,6,""],
"van_two_way_cost": [14,"",16],
"subway_cost": [11,22,33],
"bus_cost": [21,22,23]
}
]
}
How to compare van_one_way_cost and van_two_way_cost based on the condition and populate the cost array?
Jolt transforamtion
[
{
"operation": "shift",
"spec": {
"*": "&",
"destination": {
"*": {
"destinationName": {
"*": {
"@": [
"destination.[#2].destName",
"transportList.transportDestination.[#2].name"
]
}
},
"taxi_cost": {
"*": {
"@": [
"transportList.transportDestination.[&1].transports.[&1].cost"
]
}
},
"van_one_way_cost": {
"*": {
"@": [
"destination.[#2].owVanFee",
"transportList.transportDestination.[&1].transports.[&1].cost"
]
}
},
"van_two_way_cost": {
"*": {
"@": [
"destination.[#2].twVanFee",
"transportList.transportDestination.[&1].transports.[&1].cost"
]
}
},
"subway_cost": {
"*": {
"@": [
"transportList.transportDestination.[&1].transports.[&1].cost"
]
}
},
"bus_cost": {
"*": {
"@": [
"transportList.transportDestination.[&1].transports.[&1].cost"
]
}
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": {
"vanFee": ["=toInteger(@(1,owVanFee))", "=(@(1,twVanFee))"]
}
}
}
},
{
"operation": "remove",
"spec": {
"*": {
"*": {
"owVanFee": "",
"twVanFee": ""
}
}
}
}
]
Output I am getting
{
"destination" : [
{
"destName": "dest1",
"vanFee" : 7
},
{
"destName": "dest2",
"vanFee" : 6
},
{
"destName": "dest3",
"vanFee" : 16
}
],
"transportList" : {
"transportDestination" : [
{
"name": "dest1",
"transports" : [ {
"cost" : [ 10, 7, 14, 11, 21 ]
}]
},
{
"name": "dest2",
"transports" : [ {
"cost" : [ 20, 6, "", 22, 22 ]
}]
},
{
"name": "dest3",
"transports" : [ {
"cost" : [ 30, "", 16, 33, 23 ]
}]
}
]
}
}
Expexted Output
{
"destination" : [
{
"destName": "dest1",
"vanFee" : 7
},
{
"destName": "dest2",
"vanFee" : 6
},
{
"destName": "dest3",
"vanFee" : 16
}
],
"transportList" : {
"transportDestination" : [
{
"name": "dest1",
"transports" : [ {
"cost" : [ 10, 7, 11, 21 ]
}]
},
{
"name": "dest2",
"transports" : [ {
"cost" : [ 20, 6, 22, 22 ]
}]
},
{
"name": "dest3",
"transports" : [ {
"cost" : [ 30, 16, 33, 23 ]
}]
}
]
}
}
2
Answers
You can try the following spec:
You can use the following transformation specs :