Mastering JSON Patch: Essential Examples

JSON Patch, also known as JSON Merge Patch or JSON Modification Patch, is an efficient way to update a JSON document without having to resend the entire documen...

JSON Patch, also known as JSON Merge Patch or JSON Modification Patch, is an efficient way to update a JSON document without having to resend the entire document. It's particularly useful in applications that consume heavy data like web APIs. Instead of transferring the whole updated data, JSON Patch sends only the differences, reducing bandwidth usage and speeding up the process. Let's dive into some practical examples to understand JSON Patch better.

JSON Definition
JSON Definition

JSON Patch uses a simple array of operations to describe how to change a resource. The format includes only the operations necessary to transform the initial to the final state, making it compact and easy to understand. The possible operations are 'add', 'remove', 'replace', 'move', and 'copy'.

JSON Explained
JSON Explained

Understanding Operations

Before we look at examples, let's understand each operation briefly.

Json File free icons designed by Smashicons
Json File free icons designed by Smashicons

'add': Adds a new value to the document. 'remove': Removes a value from the document. 'replace': Replaces an existing value with a new one. 'move': Moves a value within the document. 'copy': Creates a new value by copying an existing one.

Changing Values

JSON prompting for LLMs
JSON prompting for LLMs

The 'add', 'remove', and 'replace' operations are fundamental to JSON Patch. They allow you to manipulate the values of a JSON document efficiently.

For instance, suppose we have the following initial JSON document:

{
  "store": {
    "book": [
      { "category": "fiction", "price": 32 },
      { "category": "ciences", "price": 25 }
    ]
  }
}

To update the price of the second book from 25 to 40, we can use the 'replace' operation:

Javascript PHP JSON Example
Javascript PHP JSON Example

[ { "op": "replace", "path": "/store/book/1/price", "value": 40 } ]

Adding and Removing Paths

To demonstrate the 'add' and 'remove' operations, let's add a new book with category 'cooking' and price 30, and then remove the first book from the list.

Adding a new book:

What is JSON? (Data Format Explained Simply) 📦📄
What is JSON? (Data Format Explained Simply) 📦📄

[ { "op": "add", "path": "/store/book/-", "value": { "category": "cooking", "price": 30 } } ]

Removing the first book:

[ { "op": "remove", "path": "/store/book/0" } ]

More Advanced Operations

Patch: Johnson Valley
Patch: Johnson Valley
JSON: Essential Guide for Web Apps & APIs
JSON: Essential Guide for Web Apps & APIs
JSON Formatter for app development, frontend coding, and error detection
JSON Formatter for app development, frontend coding, and error detection
a simple json object is displayed on the screen
a simple json object is displayed on the screen
JSON: Modern Data Exchange Explained
JSON: Modern Data Exchange Explained

Now that we've covered the basics, let's look at 'move' and 'copy' operations, which are slightly more complex but offer powerful functionality.

Moving Values

The 'move' operation allows you to rearrange elements in an array or change the position of a key in an object. Let's move the 'cooking' book to the first position in our list:

[ { "op": "move", "from": "/store/book/2", "path": "/store/book/0" } ]

Copying Values

The 'copy' operation lets you duplicate a value, optionally at a new location. Let's copy the 'cooking' book and add it as a new book with the same category and new price 28:

[ { "op": "copy", "from": "/store/book/0", "path": "/store/book/-", "value": { "price": 28 } } ]

JSON Patch is a powerful tool for efficiently updating JSON documents. It's particularly useful in applications that consume heavy data, helping to reduce bandwidth usage and speed up the update process. Understanding and utilizing these operations can significantly enhance your application's performance.

Load Site Average 0,422 sec