🔄JSON Patch Replace Operation
Update existing values in JSON documents
What is the Replace Operation?
The replace operation updates the value at the specified path with a new value. The path must point to an existing value - use add for creating new values. This is semantically equivalent to remove followed by add.
Syntax
Examples
Replace Primitive
Update a simple value
Patch:
Before:
After:
Replace Object
Replace an entire nested object
Patch:
Before:
After:
Replace Array Element
Update a specific array item
Patch:
Before:
After:
Real World: Flip a Feature Flag
Switch a boolean setting in one operation
Patch:
Before:
After:
Common Use Cases
- Updating user profile information
- Changing configuration values
- Modifying prices or quantities
- Updating status fields
Best Practices
- Path must exist - use add for new values
- Replaces the entire value, including nested structures
- Use for clarity over add when updating existing values
Replace Operation FAQ
What is the difference between replace and add?
replace requires the path to already exist; add can create new keys. On an existing object key both would update the value, but replace documents the intent and fails loudly if the field is missing.
Can replace change the type of a value?
Yes - replace swaps the entire value, so a string can become an object or a number can become an array. The whole old value is discarded.
How do I change just one field of a nested object?
Point the path at the field, not the object: replace /address/city only touches city. Replacing /address would wipe every other field in that object.
Try the Replace Operation Now
Use our free online JSON Patch generator to create replace patches instantly
🚀 Open JSON Patch Generator