It is often used when systems want to send incremental updates instead of resending a full object every time. For example, a server can stream small patches over SSE or WebSockets to update a client-side document, chat state, or tool result in place. This pattern also shows up in streaming AI chat UIs, where the server may send structured deltas as new content arrives.
In this question, implement applyJsonPatch(document, operations) which applies a list of patch operations to a nested object and returns the patched result.
This question is intentionally simplified:
Only add, remove, and replace operations need to be supported.
The document only contains plain objects and JSON primitive values. Arrays are out of scope.
Every path uses /-delimited object keys such as /user/name.
Inputs are guaranteed valid.
Examples
applyJsonPatch(
{
user:{
name:'Alice',
role:'admin',
},
},
[
{op:'replace',path:'/user/name',value:'Bob'},
{op:'add',path:'/user/active',value:true},
],
);
// {
// user: {
// name: 'Bob',
// role: 'admin',
// active: true,
// },
// }
applyJsonPatch(
{
settings:{
theme:'dark',
locale:'en-US',
},
},
[{op:'remove',path:'/settings/locale'}],
);
// {
// settings: {
// theme: 'dark',
// },
// }
Arguments
applyJsonPatch(document, operations) accepts the following arguments:
Argument
Type
Description
document
Object
A nested object containing JSON primitive values or other plain objects.
operations
Array
A list of patch operations to apply in order.
Each operation has one of the following shapes:
{op:'add',path: string,value: unknown }
{op:'remove',path: string }
{op:'replace',path: string,value: unknown }
Returns
Returns a new object with all operations applied in order.
Notes
add creates or overwrites the final property at path.
remove deletes the final property at path.
replace overwrites the existing property at path.
Paths in this question are always non-empty and always point to properties inside the object.