PATCH Operation Format
Overview
This article provides information on the procedure for performing partial updates.
The PATCH operation allows partial updates to a resource, making it a more efficient alternative to PUT when only certain fields need to be modified. We adopt the JSON Merge Patch format (as defined in RFC 7396), which provides a straightforward and human-readable way to represent changes. Unlike the JSON Patch format, which relies on an array of operations (add, remove, replace), JSON Merge Patch requires simply submitting the modified parts of the resource in standard JSON.
Key Characteristics of JSON Merge Patch:
- Simpler Syntax: Changes are expressed as a regular JSON object with updated key-value pairs.
- Idempotent: Reapplying the same PATCH request results in the same state.
- Field Removal: Fields can be removed by setting their value to
null.
Basic Examples
Imagine a resource representing a user profile, structured as follows:
Example 1: Updating Fields
To change the email and emailVerified fields, the PATCH request body would be:
Resulting resource after applying the patch:
Example 2: Removing Fields
To remove the firstName attribute field entirely:
Resulting resource:
You can combine updates and deletes in a single PATCH request.
Array Update Examples
JSON Merge Patch handles arrays differently than objects. With JSON Merge Patch, an array is treated as a whole, meaning it will replace the entire array rather than modifying individual elements.
Key Considerations for Arrays
- Array Overwrites: JSON Merge Patch replaces an entire array when a key maps to a new array.
- No In-Place Operations: You cannot directly update, add, or remove individual elements in an array. Instead, you provide the entire updated array.
- Empty Arrays: To clear an array, provide an empty array (
[]) as the value.
Let’s consider the externalGroups array:
Example 1: Replace Entire Array
To update the externalGroups array to ["admin","Group1Example","Group2Example","Group3Example"]:
Resulting resource:
Example 2: Clear an Array
To clear an array:
Resulting resource:
Example 3: Partial Updates are Not Supported for Arrays
If you attempt to modify a single element in the externalGroups array, such as changing "admin" to "client_admin", you must provide the entire updated array. For example:
Resulting resource: