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. 20145312 《网络对抗》PC平台逆向破解:注入shellcode和 Return-to-libc 攻击实验

    20145312 <网络对抗>PC平台逆向破解:注入shellcode和 Return-to-libc 攻击实验 注入shellcode 实验步骤 1. 准备一段Shellcode 2. ...

  2. 20144303石宇森《网络对抗》逆向及Bof基础

    20144303石宇森<网络对抗>逆向及Bof基础 一.实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回 ...

  3. 安装Git【转】

    本文转载自:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 最早Git是在Linu ...

  4. 学习Zookeeper之第3章Zookeeper内部原理

    第 3 章 Zookeeper 内部原理 3.1 选举机制 3.2 节点类型 3.3 stat 结构体 3.4 监听器原理   1)监听原理详解   2)常见的监听 3.5 写数据流程 第 3 章 Z ...

  5. POJ 2195 Going Home(最小费用最大流)题解

    题意:给你一张图,有k个人和k个房子,每个房子只能住一个人,每个人到某一房子的花费为曼哈顿距离,问你让k个人怎么走,使他们都住房子且花费最小. 思路:我们把所有人和超级源点相连,流量为1花费为0,所有 ...

  6. POJ 1239 Increasing Sequences(经典的两次dp)

    http://poj.org/problem?id=1239 题意:给出一串序列,现在要添加逗号作为分隔符,使得序列是递增序列,然后让最后一个数尽量小,第一个数尽量大. 思路:先从头到尾进行一次dp, ...

  7. 组学航母----OMICtools

    OMICtools可谓是组学研究的航空母舰,其收集了基因组学.转录组学.蛋白质组学和代谢组学等分析研究常用的4400余个工具和数据库.它允许用户submit自己的工具/数据库,每一个上传的工具/数据库 ...

  8. mac 下安装 express

    express为js的后端框架, 终端 >>>   npm install -g express-generator 然后cd到您要创建项目的目录之下,输入 >>> ...

  9. CSS再学习 之背景色 背景图片

    背景色 p {background-color: gray;} 如果您希望背景色从元素中的文本向外少有延伸,只需增加一些内边距. p {background-color: gray; padding: ...

  10. Mysql语句转义

    String sqlStr = "SELECT * FROM t_sys_dic WHERE idPath LIKE" + "'" + "/19/20 ...