json JOLT -过滤器嵌套数组

alen0pnh  于 2023-06-07  发布在  其他
关注(0)|答案(1)|浏览(197)

新来这里颠簸,并有这个任务是有点太多了我。我有下面的输入

{
  "Users": [
    {
      "ID": "1",
      "PrivateInfo": [
        {
          "InfoId": "ID1-II1",
          "statuses": [
            "ACTIVE"
          ]
        },
        {
          "InfoId": "ID1-II2",
          "statuses": [
            "ACTIVE",
            "INACTIVE"
          ]
        },
        {
          "InfoId": "ID1-II3",
          "statuses": [
            "IN_PROGRESS"
          ]
        }
      ]
    },
    {
      "ID": "2",
      "PrivateInfo": [
        {
          "InfoId": "ID2-II1",
          "statuses": [
            "ACTIVE",
            "SUSPENDED"
          ]
        },
        {
          "InfoId": "ID2-II2",
          "statuses": [
            "ACTIVE",
            "INACTIVE"
          ]
        },
        {
          "InfoId": "ID2-II3",
          "statuses": [
            "IN_PROGRESS"
          ]
        }
      ]
    },
    {
      "ID": "3",
      "PrivateInfo": [
        {
          "InfoId": "ID3-II1",
          "statuses": [
            "ACTIVE"
          ]
        },
        {
          "InfoId": "ID3-II2",
          "statuses": [
            "ACTIVE"
          ]
        }
      ]
    }
  ]
}

要求是,对于每个UsersPrivateInfo,只保留任何PrivateInfo,其中statuses包含onlyACTIVE,没有其他内容。
如果这样的Users包含非空的PrivateInfo,则保留该User,否则从最终结果中删除该User。
最终结果应该如下所示

{
  "Users": [
    {
      "ID": "1",
      "PrivateInfo": [
        {
          "InfoId": "ID1-II1",
          "statuses": [
            "ACTIVE"
          ]
        }
      ]
    },
    {
      "ID": "3",
      "PrivateInfo": [
        {
          "InfoId": "ID3-II1",
          "statuses": [
            "ACTIVE"
          ]
        },
        {
          "InfoId": "ID3-II2",
          "statuses": [
            "ACTIVE"
          ]
        }
      ]
    }
  ]
}

非常感谢你的帮助!!

xdyibdwo

xdyibdwo1#

您可以使用以下规范

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "Users": {
        "*": {
          "PrivateInfo": {
            "*": {
              "statuses": "=join('',@(1,&))" // concatenate the components of the array to get the case with ACTIVE only
            }
          }
        }
      }
    }
    },
  {
    "operation": "shift",
    "spec": {
      "Users": {
        "*": {
          "PrivateInfo": {
            "*": {
              // "@(2,ID)": "&4[@(3,ID)].&",
              "statuses": {
                "ACTIVE": { // filter out by ACTIVE
                  "@2": "&6.@(5,ID).&4[&3]" // group the objects by the ID values under the node of the array "Users"
                    // where &6 going six levels up the tree to reach and get the key literal "Users" 
                },
                "*": { // and vanish the rest
                  "": ""
                }
              }
            }
          }
        }
      }
    }
    },
  { // add "ID" attributes to each object while making the JSON array of objects 
    "operation": "shift",
    "spec": {
      "Users": {
        "*": {
          "*": {
            "$1": "&3[#3].ID",
            "*": {
              "*": "&4[#4].&2[&1].&",
              "statuses": "&4[#4].&2[&1].&[]" // add array wrapper
            }
          }
        }
      }
    }
  }
]

其中条件为only "ACTIVE"others(* 即使它可能包含“ACTIVE”沿着其他组件 *)在"statuses"数组在modify转换中处理后,在shift转换中使用。

相关问题