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

