如何在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
相关产品推荐
相关产品推荐

