You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Apache NiFi的InvokeHTTP与HandleHttpResponse间修改JSON数据

Apache NiFi JSON数据修改实现方案

需求概述

现有NiFi流程:HandleHttpRequest -> InvokeHTTP -> HandleHttpResponse,调用后会获取第三方API的JSON数据并返回。需在InvokeHTTP与HandleHttpResponse之间添加处理器,对JSON做以下修改:

  1. 根节点删除menuTempId、conceptId、menuId、updatedAt字段
  2. categories数组仅保留id=368的分类项
  3. 保留的分类项删除isHidden、is_toggle字段
  4. 分类下的products数组中,每个产品删除services、inSide字段

原JSON结构

{
   "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
            }
         ]
      }
   ]
}

期望JSON结构

{
   "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"
            }
         ]
      }
   ]
}

实现方案:使用JoltTransformJSON处理器

无需结合UpdateAttribute,直接用JoltTransformJSON即可完成所有修改,以下是具体配置:

1. 添加处理器

在InvokeHTTP和HandleHttpResponse之间拖入JoltTransformJSON处理器,连接成功关系。

2. 配置Jolt转换规则

打开处理器配置界面,找到Jolt Specification属性,填入以下规则:

[
  // 第一步:筛选出id=368的分类项,同时保留根节点需要的字段
  {
    "operation": "shift",
    "spec": {
      "id": "id",
      "currency": "currency",
      "language": "language",
      "countryId": "countryId",
      "version": "version",
      "categories": {
        "*": {
          "@(1,id)": {
            "368": {
              "@2": "categories[]"
            }
          }
        }
      }
    }
  },
  // 第二步:清理分类和产品中的冗余字段
  {
    "operation": "shift",
    "spec": {
      "id": "id",
      "currency": "currency",
      "language": "language",
      "countryId": "countryId",
      "version": "version",
      "categories": {
        "*": {
          "id": "categories[&1].id",
          "position": "categories[&1].position",
          "name": "categories[&1].name",
          "products": {
            "*": {
              "id": "categories[&3].products[&1].id",
              "position": "categories[&3].products[&1].position",
              "name": "categories[&3].products[&1].name"
            }
          }
        }
      }
    }
  }
]

3. 基础配置说明

  • Input JSON Attribute:保持默认content(InvokeHTTP返回的JSON默认存储在该属性中)
  • Output JSON Attribute:保持默认content,修改后的JSON会直接覆盖原内容,无需额外处理即可传给HandleHttpResponse返回

4. 验证调试

启动处理器后,用Postman调用原接口检查返回结果。如果不符合预期,可在JoltTransformJSON后添加LogAttribute处理器,查看content属性的实际值,定位转换规则的问题。

内容的提问来源于stack exchange,提问作者Neha Verma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 16:08:17