Rajesh Kumar singh
Rajesh Kumar singh

Reputation: 45

With JOLT need to Transform JSON object with If Then Else Condition

Below is my Input Json

[
  {
    "correlationId": "12345",
    "payloadFormat": "Money",
    "payload": {
      "DE35": "123654ABC54678",
      "DE45": "898454PQR54678"
    }
  }
]

I need to Trnsaform this json in such a way if DE35 has value say of substring 6-9 then take DE35 value in Transform Json, if DE35 do not have value then Take DE45 value in transform Json.

So output will be if DE35 Available then

[ {
  "COR_ID" : "12345",
  "payloadFormat" : "Money",
  "EXT_SERV_CD":"ABC"
} ]

if DE35 is not available then the output should be

[ {
  "COR_ID" : "12345",
  "payloadFormat" : "Money",
  "EXT_SERV_CD":"PQR"
} ]

I am using Below JOLT spec to do Transformation but it is not working.

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@": "&",
        "payload": {
          "DE|DE35": "&2.payload.TMPDE35"
        }
      }
    }
  }, {
    "operation": "modify-default-beta",
    "spec": {
      "*": {
        "payload": {
          "DE35Val": "=substring(@(1,TMPDE35), 6, 9)"
        }
      }
    }
  },

  {
    "operation": "shift",
    "spec": {
      "*": {
        "@": "&",
        "payload": {
          "DE|DE45": "&2.payload.TMPDE45"
        }
      }
    }
  }, {
    "operation": "modify-default-beta",
    "spec": {
      "*": {
        "payload": {
          "DE45Val": "=substring(@(1,TMPDE45), 6, 9)"
        }
      }
    }
  },

  {
    "operation": "shift",
    "spec": {
      "*": {
        "correlationId": "[&1].COR_ID",
        "payloadFormat": "[&1].payloadFormat",
        "payload": {
          "DE35Val": {
            "ABC": {
              "DE35Val": "[#3].payload.EXT_SERV_CD"
            },
            "false": {
              "DE45Val": "[#3].payload.EXT_SERV_CD"
            }
          }
        }
      }
    }
  }
]

Please suggest where I am making mistake.

Upvotes: 0

Views: 1318

Answers (1)

kasptom
kasptom

Reputation: 2458

This spec should work for you

[
  {
    "operation": "modify-default-beta",
    "spec": {
      "*": {
        "TmpDE35": "=substring(@(1,payload.DE35), 6, 9)",
        "TmpDE45": "=substring(@(1,payload.DE45), 6, 9)"
      }
    }
  },
  {
    "operation": "modify-default-beta",
    "spec": {
      "*": {
        "Tmp35_45": "=concat(@(1,TmpDE35), @(1,TmpDE45))"
      }
    }
  },
  {
    "operation": "modify-default-beta",
    "spec": {
      "*": {
        "EXT_SERV_CD": "=substring(@(1,Tmp35_45), 0, 3)"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[&1].&",
        "Tmp*": null,
        "payload": null
      }
    }
  }
]

The solution idea:

  1. Move the substrings of the DE35, DE45 fields to the TmpDE35, TmpDE45
  2. Concatenate TmpDE35, TMPDE45 and store it in the Tmp35_45. If TmpE35 is empty then the first 3 characters will come from TmpDE45
  3. Get the substring Tmp35_45
  4. Clean up, shift

Upvotes: 1

Related Questions