You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过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": {}
    }
  }
]

配置说明

  1. Shift操作:
    • 先完整复制request1的所有字段结构到输出中,确保保留request1的所有字段框架
    • 再用request2的对应字段覆盖输出中的同名字段,实现"存在则取request2值"的规则
  2. Default操作:
    • 针对request1中存在但request2缺失的字段,按类型设置默认值:字符串字段设为空串,对象字段设为空对象

这个配置会严格遵循需求生成目标JSON结构。

内容的提问来源于stack exchange,提问作者Madhavi kurabalakota

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 08:05:59