Apache Nifi处理器在返回HandleHttpResponse之前更新JSON数据

b4qexyjb  于 2023-10-21  发布在  Apache
关注(0)|答案(3)|浏览(164)

我的Apache处理器设置是**HandleHttpRequest->InvokeHTTP->HandleHttpResponse
我正在访问Postman的一个API,它调用另一个API并获取JSON数据并返回。
现在,在
InvokeHTTP->HandleHttpResponse之间,我试图添加另一个处理器,它在返回响应之前修改数据。
我的JSON来自
InvokeHTTP**

{
   "id": 1,
   "menuTempId": 28,
   "conceptId": 252,
   "menuId": 1,
   "currency": "SAR",
   "language": "En",
   "updatedAt": 1695114353000,
   "countryId": "SA",
   "version": "v1",
   "categories": [
      {
         "position": 5,
         "id": 367,
         "name": "Our New",
         "isHidden": 0,
         "is_toggle": 0,
         "products": [
            {
               "id": 2724,
               "position": 3,
               "name": "Electric Lemonade",
               "services": null,
               "inSide": 0
            },
            {
               "id": 2725,
               "position": 4,
               "name": "Electric Lemonade 2",
               "services": null,
               "inSide": 0
            }
         ]
      },
      {
         "position": 4,
         "id": 368,
         "name": "Our New 2",
         "isHidden": 0,
         "is_toggle": 0,
         "products": [
            {
               "id": 2726,
               "position": 3,
               "name": "Electric Lemonade3",
               "services": null,
               "inSide": 0
            },
            {
               "id": 2727,
               "position": 4,
               "name": "Electric Lemonade 4",
               "services": null,
               "inSide": 0
            }
         ]
      }
   ]
}

我想把它修改成这样:

{
   "id": 1,
   "currency": "SAR",
   "language": "En",
   "countryId": "SA",
   "version": "v1",
   "categories": [
      {
         "position": 4,
         "id": 368,
         "name": "Our New 2",
         "products": [
            {
               "id": 2726,
               "position": 3,
               "name": "Electric Lemonade3"
            },
            {
               "id": 2727,
               "position": 4,
               "name": "Electric Lemonade 4"
            }
         ]
      }
   ]
}

我想从JSON数据中删除几个键。我试过使用**UpdateAttribute->JoltTransformJSON**,但它不工作。
你能帮助我了解从哪个处理器和配置我可以实现这个场景。

qlvxas9a

qlvxas9a1#

仅添加符合以下规范的 JoltTransformJSON 处理器沿着

[
  {
    "operation": "shift",
    "spec": {
      "id|currency|language|countryId|version": "&"
    }
  }
]

就够了
管道操作符的使用类似于它们(或双管道)在一些众所周知的编程语言中作为OR操作符使用,而&符号代表复制这些键的值。

编辑:可以,您也可以得到最新的输出。由于需要在products数组的对象中任意选择对象,因此使用下面的转换,例如

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "categories": "=lastElement"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "id|currency|language|countryId|version": "&",
      "categories": {
        "position|id|name": "&1[0].&",
        "products": {
          "*": {
            "id|position|name": "&3[0].&2[&1].&"
          }
        }
      }
    }
  }
]
zmeyuzjn

zmeyuzjn2#

JoltTransformJSON 中,您可以简单地使用remove操作来删除JSON中不需要的属性。

[
  {
    "operation": "remove",
    "spec": {
      "menuTempId|conceptId|menuId|updatedAt": ""
    }
  }
]
nhhxz33t

nhhxz33t3#

这个jump定义应该对你有用:

[
  {
    "operation": "shift",
    "spec": {
      "id":        "id",
      "currency":  "currency",
      "language":  "language",
      "countryId": "countryId",
      "version":   "version"
    }
  }
]

相关问题