如何根据 jolt 中的条件从数组中删除元素

问题描述 投票:0回答:1

数组中有多个项目,如果数组中还有另一个项目的 idTypDesc 作为最终 ID,我会尝试删除 idTypDesc 为 id 的项目。如果数组中没有 idTypDesc 作为最终 ID 的项目,则不要删除 idTypDesc 为 id 的项目

输入:

{
  "PartyAlternateId": {
    "item": [
      {
        "AltIdType": {
          "idTypDesc": "id",
          "idTyp": "0004"
        }
      },
      {
        "AltIdVal": "hjn",
        "AltIdType": {
          "idTypDesc": "ultra ID",
          "idTyp": "0018"
        }
      },
      {
        "AltIdType": {
          "idTypDesc": "Ultimate ID",
          "idTyp": "0018"
        }
      }
    ]
  }
}

预期输出:

{
  "PartyAlternateId": {
    "item": [
      {
        "AltIdVal": "hjn",
        "AltIdType": {
          "idTypDesc": "ultra ID",
          "idTyp": "0018"
        }
      },
      {
        "AltIdType": {
          "idTypDesc": "Ultimate ID",
          "idTyp": "0018"
        }
      }
    ]
  }
}
jolt
1个回答
0
投票

您可以选择通过在

shift
转换规范中使用 "":"" 键值对进行匹配,例如

[
  {
    "operation": "shift",
    "spec": {
      "PartyAlternateId": {
        "item": {
          "*": {
            "AltIdType": {
              "idTypDesc": {
                "id": {
                  "": ""
                },
                "*": {
                  "@2": "&6.&5.&4.&3", // replicates the expressin "PartyAlternateId.item.<index of the item array>.AltIdType"
                  "@3,AltIdVal": "&6.&5.&4.AltIdVal"
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "PartyAlternateId": {
        "item": {
          "*": "&2.&1[]"
        }
      }
    }
  }
]

网站上的演示 https://jolt-demo.appspot.com/ 是:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.