skip to Main Content

I have three scenarios:

Scenario 1: When Fee1 has a value and Fee2 is "", output attribute FinalFee should have Fee1 value
Scenario 2: When Fee1 is "" and Fee2 has a value, output attribute FinalFee should have Fee2 value
Scenario 1: When Fee1 has a value and Fee2 has a value, output attribute FinalFee should have Fee1 value

Input:

{
  "school": [
    {
      "schoolCode": ["SH","MA","KR"],
      "fee1": [3,"",5],
      "fee2": ["",10,15]
    }
  ]
}

Expected Output:

{
  "school": [
    {
      "schoolCode": "SH",
      "finalFee": 3
    },
    {
      "schoolCode": "MA",
      "finalFee": 10
    },
    {
      "schoolCode": "KR",
      "finalFee": 5
    }
  ]
}

All 3 scenarios are mentioned in the input, how to write the jolt operation for this. ifNull and ifEmpty functions did not work.

This is the spec operation I am trying to update

[
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "school": {
        "*": {
          "schoolCode": {
            "*": {
              "@": [
                "school.[#2].schoolCode"
              ]
            }
          },
          "fee1": {
            "*": {
              "@": [
                "school.[#2].finalFee" 
              ]
            }
          },
           "fee2": {
            "*": {
              "@": [
                "school.[#2].finalFee" 
              ]
            }
          }
        }
      }
    }
  }
]

2

Answers


  1. Hi Shobika you can try this spec :

    In first shift we are seperating each object of school array.
    In Second shift addding a logic of Scenario 1 & Scenario 2.

    And last operation cardinality will resolve Scenario 3 where it will pick value of finalFee from fee1 if both values are coming.

    [
      {
        "operation": "shift",
        "spec": {
          "school": {
            "*": {
              "schoolCode": {
                "*": {
                  "@": "school.[#2].schoolCode"
                }
              },
              "fee1": {
                "*": {
                  "@": "school.[#2].fee1"
                }
              },
              "fee2": {
                "*": {
                  "@": "school.[#2].fee2"
                }
              }
            }
          }
        }
      },{
        "operation": "shift",
        "spec": {
          "school": {
            "*": {
              "*": "school.[#2].&",
              "fee1": {
                "": {
                  "Trash": "Trash"
                },
                "*": {
                  "@1": "school.[#4].finalFee"
                }
              },
              "fee2": {
                "": {
                  "Trash": "Trash"
                },
                "*": {
                  "@1": "school.[#4].finalFee"
                }
              }
            }
          }
        }
      },
      {
        "operation": "cardinality",
        "spec": {
          "school": {
            "*": {
              "finalFee": "ONE"
            }
          }
        }
      }
    ]
    
    Login or Signup to reply.
  2. You can use the folowing transformation spec :

    [
      {/Rearrange the elements to resemble the expected output 
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": {
                "*": "&3[&].&1"//&3  represents the key "school" 
                               //[&] produces arraywise results along with the index values, eg. 0,1,2 
                               //&1  represents the keys of the arrays
              }
            }
          }
        }
      },
      {//keep fee1 if it's an integer, otherwise return fee2   
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "*": {
              "finalFee": ["=toInteger(@(1,fee1))", "=(@(1,fee2))"] //Elvis Operator
            }
          }
        }
      },
      {//get rid of the unneeded attributes
        "operation": "remove",
        "spec": {
          "*": {
            "*": {
              "fee*": ""
            }
          }
        }
      }
    ]
    

    Refer this page for Elvis Operator

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search