Subscribe to our
weekly update
Sign up to receive our latest news via a mobile-friendly weekly email
Discover the operations that JSON Patch supports, different use cases, and key takeaways for developers and frontend engineers.
APIs have become indispensable to modern digital businesses, not least because of their flexibility and ease of integration.
At Catchpoint, we continually strive to keep our systems updated with the latest technology & trends, including the ability to expose different APIs so that developers can use them to perform a wide variety of operations. We are excited to be soon launching our latest version of the REST API where we will be not only achieving parity with the existing version but adding new features and technical enhancements for developers dealing with APIs. One visible change to the REST API is connected to Patch operations, which can now be used for updating an entity (previously, the POST operation was the pathway for users to update entities).
In this blog, we will demonstrate how to use JSON Patch to update an entity and help you identify any issues during implementation.
This quick guide on Patch Operations covers:
Many APIs use JSON as their base content type (requests & responses). JSON is an open standard of attribute key/value pairs that is widely used in programming, and commonly for HTTP requests and responses, for example, JSON can be used for a range of operations, including POST, and PATCH.
Our API accepts standard JSON in the request body to specify updates for a resource. A typical JSON document may have different types of attributes, for example, simple attributes of string types, object types or array/list types. These attributes get updated according to different business needs. Operations involved might include adding a new object in an Array/List, removing an object, updating it completely or partially. There are similar operations for other types, such as objects/strings. A JSON Patch Document can specifically be used in (Rest API) Patch Operations, in which a user may want to add/replace/remove part of the (existing) JSON document (a business entity).
The Newton soft JSON Patch Document NuGet package (Microsoft.AspNetCore.Mvc.NewtonsoftJSON) supports different operations for different scenarios. However, in some instances, it behaves differently – which we will cover.
A basic understanding of Rest API paradigms, .Net Core and C#.
Typically, developers must deal with a range of different types of JSON document and apply a variety of Patch operations when doing so. JSON Patch Document operations behave differently with different data types. This article attempts to demonstrate how you can simplify handling different operations with JSON Patch Documents, along with highlighting some of its limitations.
We will look at how to apply a range of different Patch Operations on a range of object types, such as:
The .Net core solution “PatchSamples.sln” has an API project and an API.Test project. Download the patch examples (zip).
PatchSampleController has different patch methods. The model Animal represents the animal.JSON document below. We will take a complex JSON document to demonstrate a series of different operations.
JSON Patch is a format for specifying updates to be applied to a resource. A JSON Patch document has an array of operations. Each operation identifies a particular type of change. Examples of such changes include adding an array element or replacing a property value. (as defined @Microsoft Documentation).
Before exploring different use cases, it is important to understand the JSON Patch Document request schema. The key properties are as follows:
The root of the schema is a JSON Array, which contains a list of complex objects called an operation. The operation object has the following key properties:
Within a JSON Patch document, a single request can have multiple operations.
Now let's look at some different possible use cases, starting with:
This includes:
The JSON Patch Document request to add a “name” property needs a value, “New Name”:
The JSON Patch document request to add an object as a value to “animalType” property:
If the property is a child of another object, then a parent of that object must be initialized. In the above example, if the “description” is null, the operation will fail and throw this error:
The JSON Patch document request to add an object at the end of an already initialized or pre-populated Array/List type of property:
The JSON Patch Document request to add an Array/List type of property, which is not initialized or pre-populated.
Remove operations include:
This will remove the property value on the specified path, if there is no value or if the value is already null, it won’t throw any errors:
The path should exist in the document you want to apply a patch to, otherwise it will throw an error:
The request below will remove an item from an Array/List type of element of the Patch document:
The Replace operation is actually a series of two operations: “Remove” & “Add”. This operation will remove the value specified on the path and then add the new value to it. Therefore, you can relatively request only an add operation:
The below request with “-” (hyphen) at the end of the path will remove the last element of a List. The hyphen can be replaced by an index value for a specific element:
To replace an Array/List property with another one:
Move/Copy has the same type of request and acts similarly except for one operation:
This is the test used to check the “value” specified at the “path” field. If the check succeeds, it will return “200 OK”. Otherwise, it will return, “400 Bad Request”. This operation can be used before updating any filed ones.
This operation is useful when clubbed with multiple operations to check a condition before executing the next operation:
There can be multiple operations in a single request, and they can all be of different types.
The JSON Patch NuGet provides a list of supported operations that can be used to allow specific operations that an API endpoint can support. It can also be extended and mapped with other roles (through some customization):
You need to reference an assembly for OperationType - Microsoft.AspNetCore.JSON Patch.
You can use JSON Patch document with dynamic objects as well:
You can create an instance of the JSON Patch Document and all the operations that we have discussed so far as methods of this class.
The JSON Patch doument is also handy when it comes to changing a JSON document.
Or:
I hope if you are new to HTTP Patch, this blog has helped you gain a foundational knowledge base around JSON Patch and if you are already using it, it will deepen and refine your technical knowhow.