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

如何在NiFi中使用JoltTransformJSON修改指定JSON字段名

Jolt字段重命名转换方案

需求说明

  • 当JSON数组中的对象包含ServiceFamily字段时,将其键名修改为Tag1
  • 当对象包含PublisherName字段时,将其键名修改为Tag2
  • 其余字段保持原有键名和值不变

输入示例

[
  {
    "ServiceFamily": "Compute",
    "CostAllocationRuleName": null,
    "benefitId": null,
    "benefitName": null
  },
  {
    "PublisherName": "Microsoft",
    "ChargeType": "Usage",
    "Frequency": "UsageBased",
    "PricingModel": "OnDemand",
    "benefitName": null
  }
]

期望输出示例

[
  {
    "Tag1": "Compute",
    "CostAllocationRuleName": null,
    "benefitId": null,
    "benefitName": null
  },
  {
    "Tag2": "Microsoft",
    "ChargeType": "Usage",
    "Frequency": "UsageBased",
    "PricingModel": "OnDemand",
    "benefitName": null
  }
]

Jolt转换规则

以下是实现上述需求的Jolt规范:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "ServiceFamily": "[&1].Tag1",
        "PublisherName": "[&1].Tag2",
        "*": "[&1].&"
      }
    }
  }
]

规则解释

  • operation: "shift":通过shift操作完成字段的映射与重命名
  • "*":遍历输入数组中的每一个对象,&1代表当前对象在数组中的索引位置
  • "ServiceFamily": "[&1].Tag1":将当前对象的ServiceFamily字段值映射到新键Tag1下,保留在原数组位置
  • "PublisherName": "[&1].Tag2":同理,将PublisherName字段重命名为Tag2
  • "*": "[&1].&":匹配所有未指定的字段,保持原键名(&表示原键名)和值不变,保留在原数组位置

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 22:05:52