jsonjoltelvis-operator

Jolt Mapping handling null and empty strings


I have the following input JSON value :

{
  "A": {
    "B": [
      {
        "tempName": "rq3ewas",
        "name": "test1"
      },
      {
        "tempName": "",
        "name": "test2"
      },
      {
        "tempName": null,
        "name": "test3"
      },
      {
        "tempName": "test4"
      }
    ]
  }
}

and want to get the following output based on ;

scenario1: If "tempname" is not null, then set the value of "name" to "tempName".

scenario2: If "tempname" is null, then keep the default value of "name" without changing it.

scenario3: If "tempname" is empty string(""), then keep the default value of "name" without changing it

scenario4: But If "tempname" is not present, then keep the default value of "name"

Except first scenario, last three are not working. If the code is modified last three works and first scenario is getting failed.

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "A": {
        "B": {
          "*": {
            "name": "=firstNotNull(@(1,tempName), @(2,name))"
          }
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "A": {
        "B": {
          "*": {
            "tempName": ""
          }
        }
      }
    }
  }
]

O/p:

{
  "A" : {
    "B" : [ {
      "name" : "rq3ewas"
    }, {
      "name" : null
    }, {
      "name" : ""
    }, {
      "name" : "test4"
    } ]
  }
}

Expected O/p:

{
  "A" : {
    "B" : [ {
      "name" : "rq3ewas"
    }, {
      "name" : "test2"
    }, {
      "name" : "test3"
    }, {
      "name" : "test4"
    } ]
  }
}
[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "A": {
        "B": {
          "*": {
            "name": "=firstNotNull(@(1,tempName), @(2,name))"
          }
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "A": {
        "B": {
          "*": {
            "tempName": ""
          }
        }
      }
    }
  }
]

Solution

  • Below code is working and giving the expected output.

    [ {
      "bI" : {
        "id" : "t1",
        "locale" : [ "id_ID", "id_ID" ],
        "tempName" : null,
        "name" : "Test1",
        "isActive" : false,
        "pT" : "Re"
      },
      "id" : "t1",
      "ContextID" : "id_ID",
      "cf" : {
        "name" : "asd"
      },
      "dE" : {
        "sS" : "arte"
      },
      "mk" : { },
      "pd" : { }
    } ]