寻求JOLT转换方案:用实体替换引用ID
需求:JOLT转换实现引用ID替换为对应实体
输入JSON
{ "users": [ { "id": 1, "firstname": "Max" }, { "id": 4, "firstname": "Mel" } ], "special-role": { "id": 4 } }
期望输出JSON
{ "special-role" : { "firstname": "Mel" } }
问题卡点
之前尝试的规则里用了固定值4来匹配users中的数据,但实际需要动态引用special-role.id的值,不知道怎么替换这个固定参数。
正确的JOLT转换规则
[ { "operation": "shift", "spec": { "users": { "*": { "firstname": "user_map.@(1,id).firstname" } }, "special-role": { "id": "target_id" } } }, { "operation": "shift", "spec": { "user_map": { "*": { "firstname": { "@(3,target_id)": { "$": "special-role.firstname" } } } } } }, { "operation": "remove", "spec": { "user_map": "", "target_id": "" } } ]
规则说明
- 第一步
shift:把users数组转成以id为键的user_map结构,同时把special-role的id暂存到target_id,方便后续动态匹配。 - 第二步
shift:遍历user_map的所有键,当键和target_id的值一致时,将对应的firstname赋值给special-role.firstname。 - 第三步
remove:清理中间生成的user_map和target_id,只保留最终需要的结构。
内容的提问来源于stack exchange,提问作者Alex Schmidt
相关产品推荐
相关产品推荐

