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

如何在Apache NiFi中用Jolt规范拆分JSON数组并处理InvoiceMonth

Jolt规范实现多数组字段JSON拆分需求

需求说明

当credit对象包含多字段数组时,需将原JSON拆分为多个子JSON:

  • 第一个子JSON保留原InvoiceMonth值
  • 其余子JSON的InvoiceMonth值设为"0"
  • 每个子JSON的credit对象对应原数组中对应索引的单个值

Jolt转换规范

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "credit": {
          "*": {
            "*": {
              "@(3,InvoiceMonth)": "[&1].InvoiceMonth",
              "@(2,resource_name)": "[&1].resource_name",
              "@(2,resource_global_name)": "[&1].resource_global_name",
              "@(2,label)": "[&1].label",
              "@": "[&1].credit.&(2)"
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "InvoiceMonth": ["=notEquals(@(1), 0)", "@(0)", "0"]
      }
    }
  }
]

输入示例

[
  {
    "InvoiceMonth": "202306",
    "credit": {
      "name": [
        "7828credit:0",
        "2738credit:0"
      ],
      "amount": [
        "2.5E-5",
        "5.0E-5"
      ],
      "full_name": [
        "Committed USAGE",
        "Committed USAGE "
      ],
      "id": [
        "7822credit:0",
        "2732credit:0"
      ],
      "type": [
        "COMMITTEDDISCOUNT",
        "COMMITTEDDISCOUNTBASE"
      ]
    },
    "resource_name": "projects-2",
    "resource_global_name": "//https.googleapis.com/",
    "label": ""
  }
]

输出示例

[
  {
    "InvoiceMonth": "202306",
    "credit": {
      "name": "7828credit:0",
      "amount": "2.5E-5",
      "full_name": "Committed USAGE",
      "id": "7822credit:0",
      "type": "COMMITTEDDISCOUNT"
    },
    "resource_name": "projects-2",
    "resource_global_name": "//https.googleapis.com/",
    "label": ""
  },
  {
    "InvoiceMonth": "0",
    "credit": {
      "name": "2738credit:0",
      "amount": "5.0E-5",
      "full_name": "Committed USAGE ",
      "id": "2732credit:0",
      "type": "COMMITTEDDISCOUNTBASE"
    },
    "resource_name": "projects-2",
    "resource_global_name": "//https.googleapis.com/",
    "label": ""
  }
]

规范说明

  1. Shift操作:遍历credit下的所有数组字段,按索引将对应值分配到新的数组对象中,同时携带resource_name、resource_global_name等非credit字段的值。
  2. Modify操作:通过索引判断,第一个子JSON(索引0)保留原InvoiceMonth值,其余子JSON将InvoiceMonth覆盖为"0"。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 12:37:15