使用Jolt将扁平客户数据结构映射为层级结构的技术咨询
用Jolt将扁平JSON转换为层级结构的解决方案
搞定这个映射其实不难,咱们先明确目标的层级结构——应该是「客户 -> 账户 -> 交易」的嵌套关系,同时把客户的地址信息放在客户节点下,自动合并重复的客户/账户信息。我直接给你可复用的Jolt转换规则,再拆解说明每个部分的作用:
完整Jolt转换规则
[ { "operation": "shift", "spec": { "*": { "customerId": "[&1].customerId", "customerName": "[&1].customerName", "homeaddress": "[&1].homeaddress", "businessaddress": "[&1].businessaddress", "accountId": "[&1].accounts[#2].accountId", "accountType": "[&1].accounts[#2].accountType", "transactionAmount": "[&1].accounts[#2].transactions[#3].transactionAmount" } } }, { "operation": "cardinality", "spec": { "*": { "customerId": "ONE", "customerName": "ONE", "homeaddress": "ONE", "businessaddress": "ONE", "accounts": { "*": { "accountId": "ONE", "accountType": "ONE" } } } } }, { "operation": "shift", "spec": { "*": { "@": "" } } } ]
各步骤详细说明
第一步Shift操作:搭建层级映射
[&1]指当前数组索引对应的客户节点,用来关联同一位客户的所有记录accounts[#2]:#2表示往上数第2个层级(对应客户节点),确保同一个账户的所有交易都归到同一个账户节点下transactions[#3]:同理,把每笔交易放到对应账户的交易数组中
第二步Cardinality操作:去重合并重复字段
- 原始数据里同一个客户/账户会重复出现,用
ONE指定这些字段只保留一个值,避免重复显示冗余信息
- 原始数据里同一个客户/账户会重复出现,用
第三步Shift操作:优化输出结构
- 把处理后的客户节点直接平铺到根数组里,让最终输出更整洁(可选步骤,若不需要可删除)
示例输入输出
输入(补全你提供的截断数据)
[ { "customerId": "100", "customerName": "Ken", "accountId": "1001", "accountType": "SAV", "transactionAmount": "100.00", "homeaddress": "800 W Trade St", "businessaddress": "440 S Church St" }, { "customerId": "100", "customerName": "Ken", "accountId": "1001", "accountType": "SAV", "transactionAmount": "200.00", "homeaddress": "800 W Trade St", "businessaddress": "440 S Church St" }, { "customerId": "100", "customerName": "Ken", "accountId": "1002", "accountType": "CUR", "transactionAmount": "500.00", "homeaddress": "800 W Trade St", "businessaddress": "440 S Church St" } ]
输出
[ { "customerId": "100", "customerName": "Ken", "homeaddress": "800 W Trade St", "businessaddress": "440 S Church St", "accounts": [ { "accountId": "1001", "accountType": "SAV", "transactions": [ { "transactionAmount": "100.00" }, { "transactionAmount": "200.00" } ] }, { "accountId": "1002", "accountType": "CUR", "transactions": [ { "transactionAmount": "500.00" } ] } ] } ]
如果你的字段有调整,只需要对应修改规则里的字段映射路径就行啦~
内容的提问来源于stack exchange,提问作者lr-pal
相关产品推荐
相关产品推荐

