Friday, 26 March 2021

Exploring JSON Patch

JSON Patch defines a JSON document structure for expressing a sequence of operations to apply to a JavaScript Object Notation (JSON) document; it is suitable for use with the HTTP PATCH method. The "application/json-patch+json" media type is used to identify such patch documents. You can refer this link (https://tools.ietf.org/html/rfc6902) for more information.

 

JSON Patch document is a JSON document that represents an array of objects, where each object represents a single operation to be applied to the target JSON document.

 

Example of json patch document

[{
    "op": "test",
    "path": "/emp/address/city",
    "value": "Bangalore"
  },
  {
    "op": "remove",
    "path": "/emp/firstName"
  },
  {
    "op": "add",
    "path": "/emp/hobbies",
    "value": ["cricket", "football"]
  },
  {
    "op": "replace",
    "path": "/emp/age",
    "value": 42
  },
  {
    "op": "move",
    "from": "/emp/address/city",
    "path": "/emp/address/state"
  },
  {
    "op": "copy",
    "from": "/emp/firstName",
    "path": "/emp/lastName"
  }
]

As you see above json document structure, "op" key represent the operation to perform.

 

Following table summarizes the operations specified in json patch specification.

 

Operation

Description

add

Add value at given location.

 

If the target location specifies an array index, a new value is inserted into the array at the specified index.

 

If the target location specifies an object member that does not already exist, a new member is added to the object.

 

If the target location specifies an object member that does exist, that member's value is replaced.

remove

removes the value at the target location.

replace

replaces the value at the target location

move

removes the value at a specified location and adds it to the target location.

copy

copies the value at a specified location to the target location.

test

tests that a value at the target location is equal to a specified value.

 

Let’s see it with an example.

 

Source json

{
  "firstName": "Krishna",
  "lastName": "Gurram",
  "hobbies": [
    "Cricket",
    "Football"
  ],
  "address": {
    "city": "Bangalore",
    "state": "Karnataka",
    "country": "India"
  }
}


target json

{
  "firstName": "Ram",
  "lastName": "Gurram",
  "hobbies": [
    "Football",
    "Chess"
  ],
  "address": {
    "city": "Amaravathi",
    "state": "Andhra Pradesh",
    "country": "India"
  }
}

Following patch document is used to transform source json to target json

[
  {
    "op":"replace",
    "path":"/firstName",
    "value":"Ram"
  },
  {
    "op":"replace",
    "path":"/hobbies/0",
    "value":"Football"
  },
  {
    "op":"replace",
    "path":"/hobbies/1",
    "value":"Chess"
  },
  {
    "op":"replace",
    "path":"/address/city",
    "value":"Amaravathi"
  },
  {
    "op":"replace",
    "path":"/address/state",
    "value":"Andhra Pradesh"
  }
]


We need to apply above operations sequentially on source json document to transform source json to target json document. In my next post, I will explain how to get this patch document json from source and target json documents.

 

Reference

https://tools.ietf.org/html/rfc6902

 


 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment