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

如何在NiFi中通过Jolt转换同步数组首个对象的Version值至其余子数组

问题描述

处理JSON数组:数组首个对象的Version字段含有效值,其余对象的Version字段为null,需将首个对象的Version值赋值给数组中所有其他对象的同名字段。

输入示例

[
  {
    "InvoiceSectionName": "kcltechsw.com",
    "AccountName": "KCLSW-AZRP-SAAS",
    "Version": 1
  },
  {
    "InvoiceSectionName": "MEU Core Team",
    "AccountName": "AAA_MEU Master Account",
    "Version": null
  },
  {
    "InvoiceSectionName": "MEU Core Team",
    "AccountName": "AAA_MEU Master Account",
    "Version": null
  },
  {
    "InvoiceSectionName": "Unassigned",
    "AccountName": "KCL_Finastra_Barita",
    "Version": null
  }
]

预期输出示例

[
  {
    "InvoiceSectionName": "kcltechsw.com",
    "AccountName": "KCLSW-AZRP-SAAS",
    "Version": 1
  },
  {
    "InvoiceSectionName": "MEU Core Team",
    "AccountName": "AAA_MEU Master Account",
    "Version": 1
  },
  {
    "InvoiceSectionName": "MEU Core Team",
    "AccountName": "AAA_MEU Master Account",
    "Version": 1
  },
  {
    "InvoiceSectionName": "Unassigned",
    "AccountName": "KCL_Finastra_Barita",
    "Version": 1
  }
]

实现方案

JavaScript 实现

const data = [
  {
    "InvoiceSectionName": "kcltechsw.com",
    "AccountName": "KCLSW-AZRP-SAAS",
    "Version": 1
  },
  {
    "InvoiceSectionName": "MEU Core Team",
    "AccountName": "AAA_MEU Master Account",
    "Version": null
  },
  {
    "InvoiceSectionName": "MEU Core Team",
    "AccountName": "AAA_MEU Master Account",
    "Version": null
  },
  {
    "InvoiceSectionName": "Unassigned",
    "AccountName": "KCL_Finastra_Barita",
    "Version": null
  }
];

if (data.length > 0) {
  const targetVersion = data[0].Version;
  for (let i = 1; i < data.length; i++) {
    data[i].Version = targetVersion;
  }
}

console.log(JSON.stringify(data, null, 2));

Python 实现

import json

data = [
  {
    "InvoiceSectionName": "kcltechsw.com",
    "AccountName": "KCLSW-AZRP-SAAS",
    "Version": 1
  },
  {
    "InvoiceSectionName": "MEU Core Team",
    "AccountName": "AAA_MEU Master Account",
    "Version": None
  },
  {
    "InvoiceSectionName": "MEU Core Team",
    "AccountName": "AAA_MEU Master Account",
    "Version": None
  },
  {
    "InvoiceSectionName": "Unassigned",
    "AccountName": "KCL_Finastra_Barita",
    "Version": None
  }
]

if len(data) > 0:
    target_version = data[0]["Version"]
    for item in data[1:]:
        item["Version"] = target_version

print(json.dumps(data, indent=2))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 05:05:25