skip to Main Content

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:

  1. 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.
  2. if element at index position 1 is "" and position 2 is an integer, keep that element in the array and remove position 1.
  3. 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


  1. You can try the following spec:

    [
      // Merge the van one and two way cost and ignore  any empty value
      // this will generate an array for each index of none empty values
      // where in the next step we will consider only the first element of that array
      
      {
        "operation": "shift",
        "spec": {
          "destination": {
            "*": {
              "*": "&2[&1].&",
              "van_one_way_cost": {
                "*": {
                  "": null,
                  "*": {
                    "@1": "&5[&4].van_cost.&2[]"
                  }
                }
              },
              "van_two_way_cost": {
                "*": {
                  "": null,
                  "*": {
                    "@1": "&5[&4].van_cost.&2[]"
                  }
                }
              }
            }
          }
        }
      },
      // use the first element of each inex array from above with the 
      // destinationName to generate the destination object.
      // align indexes from all arrays to generate the transportationList
      {
        "operation": "shift",
        "spec": {
          "destination": {
            "*": {
              "destinationName": {
                "*": {
                  "@": "destination[#2].destName",
                  "@(2,van_cost.&[0])": "destination[#2].vanFee"
                }
              },
              "taxi_cost": {
                "*": {
                  "@(2,destinationName[&])": "transportList.transportDestination[#2].name",
                  "@": "transportList.transportDestination[#2].transports[#].costs[]",
                  "@(2,van_cost.&[0])": "transportList.transportDestination[#2].transports[#].costs[]",
                  "@(2,subway_cost[&])": "transportList.transportDestination[#2].transports[#].costs[]",
                  "@(2,bus_cost[&])": "transportList.transportDestination[#2].transports[#].costs[]"
                }
              }
            }
          }
        }
      }
    ]
    
    Login or Signup to reply.
  2. You can use the following transformation specs :

    [
      { //group the sub-arrays by "destinationName" vs. "others"  
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*ination*": { //form independent key-value pairs under object of "destination" array
                "*": "&3[#1].&(1,1)&(1,2)"
              },
              "van_*": { //distinguish the sub-arrays starting with "van" from the others 
                //and reform new arrays named "vanFee"
                "*": "tD[#1].vanFee"
              },
              "*": { //form independent key-value pairs under object of "tD" array
                "*": "tD[#1].&1"
              }
            }
          }
        }
      },
      { //unify the attributes starting with "vanFee" under common attributes 
        "operation": "modify-overwrite-beta",
        "spec": {
          "tD": {
            "*": {
              "vanFee": ["=toInteger(@(1,vanFee[0]))", "=toInteger(@(1,vanFee[1]))"]
            }
          }
        }
      },
      { //form cost arrays with four elements rather than five
        //while transferring the destination.names into the common array with the cost arrays    
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": "&2[#2].&",
              "@2,tD[&].vanFee": "&2[#2].vanFee"//distribute new index-1 elements into "destination" arrays
            }
          },
          "tD": {
            "*": {
              "@2,destination[&].destName": "transportList.transportDestination[#2].name",
              "*": "transportList.transportDestination[#2].transports[0].cost"
            }
          }
        }
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search