简体   繁体   English

如何使用 Jolt 变换从数组中删除 null

[英]How to remove null from a array using Jolt transformation

Requirement - I want to remove null values from the transformed Json using Jolt spec.要求 -我想使用 Jolt 规范从转换后的 Json 中删除null值。

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 Jolt Spec -如果hierarchyType = "MAINTAINANCE" && parents[].levelCode = "CO"然后将parents[].operationalId复制到 output

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

Actual Output -实际 Output -

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

Expected Output -预计 Output -

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

You can add an extra modify-beta transform along with recursivelySquashNulls to the current spec such as您可以在当前规范中添加额外的modify-beta变换以及recursivelySquashNulls ,例如

{
  "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).编辑:如果不删除其他可能存在的数组的所有其他null元素,请不要添加上述额外规范

Rather replace the part "request.common.hierarchyData.hrchyArray[&3].hrchyEntityId" with "request.common.hierarchyData.hrchyArray[].hrchyEntityId" stated as the innermost leaf node.而是将部分“request.common.hierarchyData.hrchyArray[&3].hrchyEntityId”替换为“request.common.hierarchyData.hrchyArray[].hrchyEntityId” ,声明为最内层叶节点。

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.通过使用&3替换,您将 go 提升到数组的级别,并开始在数组的所有三个元素中冗余漫游。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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