Home > other >  How to remove null from a array using Jolt transformation
How to remove null from a array using Jolt transformation

Time:12-11

Requirement - I want to remove null values from the transformed Json using Jolt spec.

Input -

{
  "parentResponse": {
    "parentHierarchy": [
      {
        "hierarchyType": "MAINTAINANCE",
        "parents": [
          {
            "level": "Test",
            "levelCode": "BO",
            "operationalId": "OP"
          },
          {
            "level": "Test",
            "levelCode": "BO",
            "operationalId": "OP"
          },
          {
            "level": "Contract",
            "levelCode": "CO",
            "operationalId": "DP"
          }
        ]
      }
    ]
  }
}

Jolt Spec - If hierarchyType = "MAINTAINANCE" && parents[].levelCode = "CO" then copy the parents[].operationalId to the output

[
  {
    "operation": "shift",
    "spec": {
      "parentResponse": {
        "parentHierarchy": {
          "*": {
            "hierarchyType": {
              "MAINTAINANCE": {
                "@(2,parents)": {
                  "*": {
                    "levelCode": {
                      "CO": {
                        "@(2,operationalId)": "request.common.hierarchyData.hrchyArray[&3].hrchyEntityId"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

Actual Output -

{
  "request" : {
    "common" : {
      "hierarchyData" : {
        "hrchyArray" : [ null, null, {
          "hrchyEntityId" : "DP"
        } ]
      }
    }
  }
}

Expected Output -

{
  "request" : {
    "common" : {
      "hierarchyData" : {
        "hrchyArray" : [{
          "hrchyEntityId" : "DP"
        } ]
      }
    }
  }
}

CodePudding user response:

You can add an extra modify-beta transform along with recursivelySquashNulls to the current spec such as

{
  "operation": "modify-overwrite-beta",
  "spec": {
    "*": "=recursivelySquashNulls"
  }
}

enter image description here

Edit : Don't add the above extra spec if it's the case not to remove the all other null elements of the other possibly existing array(s).

Rather replace the part "request.common.hierarchyData.hrchyArray[&3].hrchyEntityId" with "request.common.hierarchyData.hrchyArray[].hrchyEntityId" stated as the innermost leaf node.

By using &3 substitution you go up to the level of parents array, and start an extra roaming through all three elements of the array redundantly.

  • Related