1. What Is JSON Patch?
1.1 Concept
JSON Patch (RFC 6902) is a standard format for describing updates to a JSON document. It allows you to partially update a JSON object without having to send the entire object again.
In simple terms: instead of sending all the data to make a small change, you only send a “list of changes” you want to apply.
1.2 Operation
| Operation | Description |
| add | Adds a new value |
| remove | Removes a value |
| replace | Replaces an existing value |
| move | Moves a value from one path to another |
| copy | Copies a value |
| test | Tests a value (used for validation) |
1.3 Request payload
[
{
"op": "replace",
"path": "/name",
"value": "Eric"
},
{
"op": "add",
"path": "/tags/-",
"value": "developer"
}
]
2. Setup JSON Patch in ASP.NET Core
2.1 Install package
ASP.NET Core has built-in support for JSON Patch via the package:
dotnet add package Microsoft.AspNetCore.Mvc.NewtonsoftJson
2.2 Configuration
Then enable it in your Program.cs or Startup.cs:
builder.Services.AddControllers()
.AddNewtonsoftJson();
2.3 Example
Patching a User Object
public class User
{
public string Name { get; set; }
public string Email { get; set; }
public List<string> Tags { get; set; } = new();
}
And an in-memory database:
private static List<User> _users = new()
{
new User { Name = "John", Email = "john@example.com" }
};
It is also highly compatible with both SQL and NoSQL databases, and you can easily integrate it with ORMs like Entity Framework and even work seamlessly across multiple databases.
Implementing the PATCH Endpoint
[HttpPatch("{id}")]
public IActionResult PatchUser(int id, [FromBody] JsonPatchDocument<User> patchDoc)
{
if (patchDoc == null)
return BadRequest();
var user = _users.FirstOrDefault(u => u.Id == id);
if (user == null)
return NotFound();
patchDoc.ApplyTo(user, ModelState);
if (!ModelState.IsValid)
return BadRequest(ModelState);
return Ok(user);
}
This is an HTTP PATCH endpoint responsible for partially updating a user’s information (identified by their ID), instead of sending the entire object as in a PUT request.
The request body contains a JSON Patch document.
It searches for the user in the _users list (assumed to be a mock list or database).
If no user with the given ID exists, it will return 404 Not Found.
Otherwise, it applies all JSON Patch operations (add, remove, replace, move, copy, etc.) to the user object.
Example Request
PATCH /api/users/1
[
{
"op": "replace",
"path": "/name",
"value": "Michael"
},
{
"op": "add",
"path": "/tags/-",
"value": "backend"
}
]
Result:
{
"name": "Michael",
"email": "john@example.com",
"tags": [
"backend"
]
}
3. Why Should Use JSON Patch?
- Efficiency: You only send what changed.
- Flexibility: Works well with partial updates.
- Clarity: Each change is explicitly defined.
- Compatibility: Supported natively in ASP.NET Core.
4. Best Practices
Always validate input paths and values to prevent unintended changes.
Avoid exposing internal model structure directly — use DTOs for patching.
Log changes if you’re patching critical data.
For security, restrict allowed paths to editable fields only.
5. Conclusion
JSON Patch is a powerful and efficient way to apply partial updates to objects in your API.
It’s cleaner, faster, and safer than sending full payloads — especially when working with large models6