What is JSON Patch?

JSON Patch is a format for describing changes to a JSON document. It can be used to avoid sending a whole document when only a part has changed. When used in combination with the HTTP PATCH method, it allows partial updates for HTTP APIs in a standards compliant way.

The patch documents are themselves JSON documents.

JSON Patch is specified in RFC 6902 from the IETF.

Simple example

The original document

{
"baz": "qux",
"foo": "bar"
}

The patch

[
{ "op": "replace", "path": "/baz", "value": "boo" },
{ "op": "add", "path": "/hello", "value": ["world"] },
{ "op": "remove", "path": "/foo" }
]

The result

{
"baz": "boo",
"hello": ["world"]
}

How it works

A JSON Patch document is just a JSON file containing an array of patch operations. The patch operations supported by JSON Patch are “add”, “remove”, “replace”, “move”, “copy” and “test”. The operations are applied in order: if any of them fail then the whole patch operation should abort.

JSON Pointer

JSON Pointer (IETF RFC 6901) defines a string format for identifying a specific value within a JSON document. It is used by all operations in JSON Patch to specify the part of the document to operate on.

A JSON Pointer is a string of tokens separated by / characters, these tokens either specify keys in objects or indexes into arrays. For example, given the JSON

{
"biscuits": [
{ "name": "Digestive" },
{ "name": "Choco Leibniz" }
]
}

/biscuits would point to the array of biscuits and /biscuits/1/name would point to "Choco Leibniz".

To point to the root of the document use an empty string for the pointer. The pointer / doesn’t point to the root, it points to a key of "" on the root (which is totally valid in JSON).

If you need to refer to a key with ~ or / in its name, you must escape the characters with ~0 and ~1 respectively. For example, to get "baz" from { "foo/bar~": "baz" } you’d use the pointer /foo~1bar~0.

Finally, if you need to refer to the end of an array you can use - instead of an index. For example, to refer to the end of the array of biscuits above you would use /biscuits/-. This is useful when you need to insert a value at the end of an array.

Operations

Add

{ "op": "add", "path": "/biscuits/1", "value": { "name": "Ginger Nut" } }

Adds a value to an object or inserts it into an array. In the case of an array, the value is inserted before the given index. The -character can be used instead of an index to insert at the end of an array.

Remove

{ "op": "remove", "path": "/biscuits" }

Removes a value from an object or array.

{ "op": "remove", "path": "/biscuits/0" }

Removes the first element of the array at biscuits (or just removes the “0” key if biscuits is an object)

Replace

{ "op": "replace", "path": "/biscuits/0/name", "value": "Chocolate Digestive" }

Replaces a value. Equivalent to a “remove” followed by an “add”.

Copy

{ "op": "copy", "from": "/biscuits/0", "path": "/best_biscuit" }

Copies a value from one location to another within the JSON document. Both from and path are JSON Pointers.

Move

{ "op": "move", "from": "/biscuits", "path": "/cookies" }

Moves a value from one location to the other. Both from and path are JSON Pointers.

Test

{ "op": "test", "path": "/best_biscuit/name", "value": "Choco Leibniz" }

Tests that the specified value is set in the document. If the test fails, then the patch as a whole should not apply.

Libraries

Libraries are available for a range of languages currently. You should check that the library you wish to use supports the RFC version of JSON Patch as there have been changes from the earlier draft versions and at the time of writing, not all libraries have been updated.

If we’re missing a library please let us know (see below)!

JavaScript

Python

PHP

Ruby

Perl

C

  • cJSON (JSON library in C, includes JSON Patch support in cJSON_Utils)

Java

Scala

C++

C#

  • Asp.Net Core JsonPatch (Microsoft official implementation)
  • Ramone (a framework for consuming REST services, includes a JSON Patch implementation)
  • JsonPatch (Adds JSON Patch support to ASP.NET Web API)
  • Starcounter (In-memory Application Engine, uses JSON Patch with OT for client-server sync)
  • Nancy.JsonPatch (Adds JSON Patch support to NancyFX)
  • Manatee.Json (JSON-everything, including JSON Patch)

Go

Haskell

Erlang

Elm

Test Suite

A collection of conformance tests for JSON Patch are maintained on Github:

github.com/json-patch/json-patch-tests

Tools

JSON Schema

JSON Schema is a way to describe JSON data formats like JSON Patch. Supporting tools and libraries can use these schemas to provide auto-completion, validation and tooltips to help JSON file authors.

http://json.schemastore.org/json-patch

 

json-patch 了解的更多相关文章

  1. 【ASP.NET Core】JSON Patch 使用简述

    JSON Patch 是啥玩意儿?不知道,直接翻译吧,就叫它“Json 补丁”吧.干吗用的呢?当然是用来修改 JSON 文档的了.那咋修改呢?比较常见有四大操作:AMRR. 咋解释呢? A—— Add ...

  2. JSON Patch

    1.前言 可以这么说的是,任何一种非强制性约束同时也没有"标杆"工具支持的开发风格或协议(仅靠文档是远远不够的),最终的实现上都会被程序员冠上"务实"的名头,而 ...

  3. 如何在ASP.NET Core中使用JSON Patch

    原文: JSON Patch With ASP.NET Core 作者:.NET Core Tutorials 译文:如何在ASP.NET Core中使用JSON Patch 地址:https://w ...

  4. [译] 在Web API 2 中实现带JSON的Patch请求

    原文链接:The Patch Verb in Web API 2 with JSON 我想在.NET4.6 Web API 2 项目中使用Patch更新一个大对象中的某个字断,这才意识到我以前都没有用 ...

  5. 用ASP.NET Core 2.0 建立规范的 REST API -- DELETE, UPDATE, PATCH 和 Log

    本文所需的一些预备知识可以看这里: http://www.cnblogs.com/cgzl/p/9010978.html 和 http://www.cnblogs.com/cgzl/p/9019314 ...

  6. streamsets http client && json parse && local fs 使用

    streamsets 包含了丰富的组件,origin processer destination 测试例子为集成了http client 以及json 处理 启动服务 使用docker 创建pipel ...

  7. kubectl 之 patch 命令

    patch命令 kubectl patch — Update field(s) of a resource using strategic merge patch Synopsis kubectl p ...

  8. Kubernetes官方java客户端之七:patch操作

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  9. Json文件解析(下)

    Json文件解析(下) 代码地址:https://github.com/nlohmann/json   从STL容器转换 任何序列容器(std::array,std::vector,std::dequ ...

  10. javaScript系列 [09]-javaScript和JSON (拓展)

    本文输出JSON搜索和JSON转换相关的内容,是对前两篇文章的补充. JSON搜索 在特定的开发场景中,如果服务器端返回的JSON数据异常复杂(可能超过上万行),那么必然就有对JSON文档进行搜索的需 ...

随机推荐

  1. CCPC-Wannafly Winter Camp Day5 (Div2, onsite)

    Replay: Dup4: 时间复杂度算不对? 一点点思路不经过验证就激动的要死? 浪费自己一个小时还浪费别人一个小时? 对1e3不敏感? 1e3 * 1e3是多少? 模拟建边跑dp不写非要写个大模拟 ...

  2. HDU4112

    对于n*m*k的方块,用手掰成1**1的那么搜需要的步骤是固定的,为n*m*k-,如果用刀切,因为可以把多块叠在一起切,所以对于长度为n的,将他切成0,所需要的步骤数位k 对于n*m*k的方块,用手掰 ...

  3. java 读CSV 和 Excel

    1.csv和excel读写对比 开发中经常遇到数据导入和导出功能,csv 和 excel是最常见的数据格式,本文比较了下csv和excel读写相同数据的效率: 测试数据格式一 用上面模板数据生成的测试 ...

  4. java第五天

    p37: 练习1 /** * Created by xkfx on 2017/2/22. */ public class DataOnly { int anInt; char aChar; publi ...

  5. 20172305 2018-2019-1 《Java软件结构与数据结构》第四周学习总结

    20172305 2018-2019-1 <Java软件结构与数据结构>第四周学习总结 教材学习内容总结 本周内容主要为书第六章内容: 列表 有序列表(元素按照元素内在特性进行排序) 无序 ...

  6. Ubuntu 14.04 下安装 TFTP 艰辛之路【转】

    本文转载自:https://blog.csdn.net/donglicaiju76152/article/details/76651210 背景 按说在Linux下安装tftp server 很简单, ...

  7. Docker 安装&基本操作

    Docker 安装 Docker 中的三个概念:镜像,容器,仓库 镜像(image):Docker 镜像就是一个只读的模板,镜像可以用来创建 Docker 容器.Docker 提供了一个很简单的机制来 ...

  8. C# .NET 开发心得

    1. 工作路径问题 1. 多项目构成的解决方案,Web APP作为启动项目时的工作路径 //当前执行的exe文件名 //C:\\Program Files\\IIS Express\\iisexpre ...

  9. h.264_javascript_资料

    1. 用ffmpeg制作推流工具,实现推流系统声音和桌面到rtmp服务器-CSDN论坛-CSDN.NET-中国最大的IT技术社区.html http://bbs.csdn.net/topics/392 ...

  10. mysql获取随机数据的方法

    order by rand() 数据多了极慢,随机性非常好,适合非常小数据量的情况. 复制代码 代码如下: SELECT * FROM table_name AS r1 JOIN (SELECT (R ...