Apply JSON Merge Patch (RFC 7396)
JSON Merge Patch applies 部分 JSON object 到更新 document — 空 values delete 关键, 其他 values 替换它们. Simpler than JSON Patch.
- 1
JSON 合并修补 (RFC 7396) 是简单合并: take 修补 object 和应用它 到目标. 为每个键 在修补: 如果值 是空, delete 键从 目标; 否则, 集合/替换键. Nested objects 是 merged recursively. 它是 更少 expressive than JSON 修补但 much simpler.
- 2 JSON 格式化器 →
格式化 the original document and the merge patch. The patch is a plain JSON object — no special structure. Format both to verify valid JSON before merging.
- 3 JSON 验证器 →
验证 the merged result. The merge is straightforward: for each key in the patch, if null → delete from target, else → set in target. Validate the result to ensure the merge produced valid data.
- 4
Limitation: Merge Patch cannot 添加空 值 (空 means delete), cannot move 或副本 values, 和 cannot 测试之前 applying. 为这些操作, use JSON Patch (RFC 6902) instead. Merge Patch 是 ideal 为简单部分 updates 从 forms 或 API PATCH requests.
常见问题
- 当到 use Merge Patch vs JSON Patch?
- Use Merge Patch 为简单部分 updates (e.g., updating 少数 fields 在资源 via PATCH /api/用户). Use JSON Patch 当你 need 到 move/副本 values, 测试 conditions, 或添加空 values. Merge Patch 是 simpler 但更少 expressive.
- 如何是 arrays handled 在 Merge Patch?
- Arrays 是 replaced 整个, 不 merged 元素-通过-元素. 如果修补有 {tags: ["", "b"]}, 整个 tags 数组是 replaced. 到修改 individual 数组 elements, use JSON 修补具有数组索引 paths.