简体   繁体   中英

How to remove null from a array using Jolt transformation

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"
        } ]
      }
    }
  }
}

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

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

在此处输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM