Unable to populate a field as expected In jolt transform Like if-else and if-else(nested)?

38 Views Asked by At

JSON Input :

{
  "code": "+91-",
  "status": "A",
  "number": "123456778",
  "country": "I",
  "pukcode": "00-766-54"
}

My Jolt Spec 1 :

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=trim",
      "mobile": "=concat(@(1,code),@(1,number),"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "eventInfo": "eventInfo",
      "mobile": {
        "": {
          "comment": [
            "if blank, do not pass mobile"
          ]
        },
        "*": {
          "@(2,mobile)": "eventData.Records.[0].mobile",
          "@(2,status)": {
            "A": {
              "#Active": "eventData.Records.[0].status"
            }
          },
          "@(2,country)": {
            "I": {
              "#India": "eventData.Records.[0].country"
            }
          },
          "@(2,pukcode)": {
            "": {
              "comment": [
                "if blank, do not pass mobile"
              ]
            },
            "*": {
              "@(2,pukcode)": "eventData.Records.[0].pukcode"
            }
          }
        }
      }
    }
  }
]

output :

{
  "eventData" : {
    "Records" : [ {
      "mobile" : "+91-123456778",
      "status" : "Active",
      "country" : "India"
    } ]
  }
}

To the above Input I need to Populate the Data based on mobile number existence then only it should populate all the fields and puk-code code should also populate when input data is prasent in the input by target to mobile. But here the Problem pukcode is not getting populate even when the field consist Input data.

My Jolt Spec 2 :

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=trim",
      "mobile": "=concat(@(1,code),@(1,number),"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "eventInfo": "eventInfo",
      "mobile": {
        "": {
          "comment": [
            "if blank, do not pass mobile"
          ]
        },
        "*": {
          "@(2,mobile)": "eventData.Records.[0].mobile",
          "@(2,status)": {
            "A": {
              "#Active": "eventData.Records.[0].status"
            }
          },
          "@(2,country)": {
            "I": {
              "#India": "eventData.Records.[0].country"
            }
          },
          "@(2,pukcode)": {
            "": {
              "comment": [
                "if blank, do not pass mobile"
              ]
            },
            "*": "eventData.Records.[0].pukcode"
          }
        }
      }
    }
  }
]

Output 2 :

{
  "eventData": {
    "Records": [
      {
        "mobile": "+91-123456778",
        "status": "Active",
        "country": "India",
        "pukcode": null
      }
    ]
  }
}

pls provide some solution to the above

Case 1 JSON Input :

{
  "code": "+91-",
  "status": "A",
  "number": "123456778",
  "country": "I",
  "pukcode": "00-766-54"
}

Expected Output 1 :

{
  "eventData": {
    "Records": [
      {
        "mobile": "+91-123456778",
        "status": "Active",
        "country": "India",
        "pukcode": "00-766-54"
      }
    ]
  }
}

Case 2 JSON Input :

{
  "code": "",
  "status": "A",
  "number": "",
  "country": "I",
  "pukcode": "00-766-54"
}

Expected Output 2 :

null
1

There are 1 best solutions below

2
Barbaros Özhan On

You can use the following transformation specs :

[
  {
    "operation": "shift",
    "spec": {
      "number": {
        "*": {
          "@2,code|@1": "mobile",
          "@2,status": { "A": { "#Active": "status" } }, // renaming occurs here
          "@2,country": { "I": { "#India": "country" } },// and here
          "@2,pukcode": "pukcode"
        },
        "": { // if "number" is null then remove every element
          "": ""
        }
      }
    }
  },
  { // concatenate the components of mobile array if it exists
    "operation": "modify-overwrite-beta",
    "spec": {
      "mobile": "=join('',@(1,&))"
    }
  },
  { // nest the current object along with some keys as desired
    "operation": "shift",
    "spec": {
      "*": "eventData.Records[0].&"
    }
  }
]