:::: MENU ::::

Implementing JSON Patch in AspNet Core

First let’s know a bit about JSON Patch.

JSON Patch is a format (identified by the media type “application/json-patch+json”) for expressing a sequence of operations to apply to a target JSON document; it is suitable for use with the HTTP PATCH method.

This format is also potentially useful in other cases in which it is necessary to make partial updates to a JSON document or to a data structure that has similar constraints (i.e., they can be serialized as an object or an array using the JSON grammar). [From JSON Patch Spec]

It is a JSON document that represents an array of objects. Each object represents a single operation to be applied to the target JSON document.

Here is an example/format of JSON Patch document:

[

{ “op”: “test”, “path”: “/a/b/c”, “value”: “foo” },

{ “op”: “remove”, “path”: “/a/b/c” },

{ “op”: “add”, “path”: “/a/b/c”, “value”: [ “foo”, “bar” ] },

{ “op”: “replace”, “path”: “/a/b/c”, “value”: 42 },

{ “op”: “move”, “from”: “/a/b/c”, “path”: “/a/b/d” },

{ “op”: “copy”, “from”: “/a/b/d”, “path”: “/a/b/e” }

]

Each Operation (op) has specific task to perform and MUST be one of “add”, “remove”, “replace”, “move”, “copy”, or “test” and any other value are consider as error.

In earlier version of AspNet (Full Framework) we need to use other third party library to achieve JSON Patch (I prefer Kevin Docx JSON Patch library) and now AspNet Core supports this feature out of the box. API are available inside “Microsoft.AspNetCore.JsonPatch” namespace. (GitHub Repo.)

Now let’s see how to implement this feature. For this article I am using AspNet Core Web API project. I am implementing only one operation i.e. replace which helps you to get started with JSON Patch and other operation too.

Scenario:

Let’s say we have conference site and attendee want to update a review they gave earlier to speaker. In this situation, we have a review object that attendee want to update and here we only need to update review section so let’s use JSON Patch to perform this task.

There are multiple approach to perform patch operation. Here are simple two way.

  1. Sending JsonPatchDocument object as a parameter
  2. Creating JsonPatchDocument at runtime

We will see this two approach in this article. For now let’s see some code.

I have created “PatchReview” action inside SpeakerController and decorated it with [HTTPPATCH] attribute.

Here i am using first approach. This method accept parameter/object of type JsonPatchDocument from request body, then we are getting actual review object that need to update based on requested reviewId but for demo purpose we are creating dummy SpeakerReview object which we assume is an actual object and applying patch to that object and lastly returning updated review. In real, scenario will be different like testing null condition, saving review after patching etc. etc..

Second approach:

In above action i am passing actual JsonPatchDocument object as a parameter so you need to remember actual format of Patch document but if you don’t want to remember those format then you can use built in corresponding operation methods to create JsonPatchDocument at runtime.

Let’s see an example of second approach. If you dig into JsonPatchDocument class there is Replace method where you have to pass path i.e. targeted property/properties of SpeakerReview object and new value then it will return JsonPatchDocument object which is equivalent to object you passed as a parameter of first approach. Now you can apply that patch document to targeted object with help of ApplyTo method. Methods are available for all JSON Patch operation to create JsonPatchDocument object. If you are interested to see actual code, here it is.

Still Confusing?? Here is Code:

That’s all for patching logic. Don’t forgot to resolve necessary namespace. 😉

Let’s see how to call this API from POSTMAN:

Note: This step is valid only if you used first technique.

Change request type to PATCH and prepare JSON object in body. Make sure that it is in JSON Patch document format. Here we are only replacing review so i am passing single operation object inside operation array but you can pass multiple operation and it will executes sequentially.

So basically with this JSON Patch document i am sending command to replace old value of review property with new value.

Tip: Make sure that your patch request header contains Content-Type of “application/json-patch+json”  MIME else your request end up with 415 Unsupported Media Type status code.

Let’s put BreakPoint in patch action and hit that send button.

Here you can compare value of review property on old and newly patched SpeakerReview object. In real application, now you need to perform remaining task but here i am returning updated object.

You can try with second approach and it will result same as above.

And this is updated SpeakerReview object as a response.

That’s it. This is how we can implement JSON Patch in AspNet Core application. My advice here would be read JSON Patch spec at least once before working with JSON Patch (takes 30 min max). You can find really good examples of different operation at last of spec.

I hope this helps you to get started with JSON Patch in AspNet Core.

Happy Coding. 🙂