根据字段内容过滤

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

我有以下输入json:

输入

{
  "temp": "1010328|1010329",
  "data": [
    {
      "product": {
        "items": [
          {
            "item_number": "1010328",
            "attributes": [
              {
                "id": "myId01",
                "type": "int"
              }
            ]
          },
          {
            "item_number": "1010329",
            "attributes": [
              {
                "id": "myId02",
                "type": "int"
              }
            ]
          },
          {
            "item_number": "1010330",
            "attributes": [
              {
                "id": "myId03",
                "type": "int"
              }
            ]
          }
        ]
      }
    }
  ]
}

在 items 数组中,应根据“temp”的值过滤项目:

期望的输出

{
  "temp": "1010328|1010329",
  "data": [
    {
      "product": {
        "items": [
          {
            "item_number": "1010328",
            "attributes": [
              {
                "id": "myId01",
                "type": "int"
              }
            ]
          },
          {
            "item_number": "1010329",
            "attributes": [
              {
                "id": "myId02",
                "type": "int"
              }
            ]
          }
         
        ]
      }
    }
  ]
}

我尝试使用分配给 nifi 变量“temp”的字符串 1010328|1010329 在 jolt 中进行正常过滤。但显然,当涉及到“过滤任务”时,jolt 不接受变量的值。

json jolt
1个回答
0
投票

您可以从将

"temp"
属性转换为数组开始,在数组的索引下循环并重新构造回其原始结构,如以下转换所示:

[
  { //convert "temp" attribute to an array with the identical name
    "operation": "modify-overwrite-beta",
    "spec": {
      "temp": "=split('\\|',@(1,&))"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "temp": "&",
      "data": {
        "*": {
          "product": {
            "items": {
              "*": {
                "*": "@(1,item_number).&5.&4.&3.&2.&" //keep the all nestine key names
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "temp": {
        "*": {
          "*": {
            "@(3,&)": ""
          }
        },
        "@": "&" //keep the "temp" array
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "temp": "&",
        "*": {
          "*": {
            "*": {
              "*": "&3[&2].&1.&[]"
            }
          }
        }
      }
    }
  },
  { //convert "temp" back to an attribute
    "operation": "modify-overwrite-beta",
    "spec": {
      "temp": "=join('|',@(1,&))"
    }
  }
]

网站上的演示使用v0.1.1的Jolt Transform演示是:

enter image description here

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