如何通过Jolt对比两个JSON对象字段并生成结果JSON?
正确Jolt配置实现JSON字段对比生成规则
需求规则
- 若
request1的字段在request2中存在,新JSON取request2的对应值 - 若
request1的字符串字段在request2中不存在,新JSON设为空字符串"" - 若
request1的对象字段在request2中不存在,新JSON设为空对象{}
输入示例
{ "request1": { "name": "", "age": "", "address": { "street": "", "city": "" }, "contact": { "phone": "" } }, "request2": { "name": "Alice", "address": { "city": "New York" } } }
期望输出
{ "name": "Alice", "age": "", "address": { "street": "", "city": "New York" }, "contact": {} }
正确Jolt转换配置
[ { "operation": "shift", "spec": { "request1": { "*": "&", "*": { "*": "&1.&" } }, "request2": { "*": "&", "*": { "*": "&1.&" } } } }, { "operation": "default", "spec": { "age": "", "address": { "street": "" }, "contact": {} } } ]
配置说明
- Shift操作:
- 先完整复制
request1的所有字段结构到输出中,确保保留request1的所有字段框架 - 再用
request2的对应字段覆盖输出中的同名字段,实现"存在则取request2值"的规则
- 先完整复制
- Default操作:
- 针对
request1中存在但request2缺失的字段,按类型设置默认值:字符串字段设为空串,对象字段设为空对象
- 针对
这个配置会严格遵循需求生成目标JSON结构。
内容的提问来源于stack exchange,提问作者Madhavi kurabalakota
相关产品推荐
相关产品推荐

