🔄JSON Patch Replace Operation

Update existing values in JSON documents
RFC 6902
replace operation

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

{ "op": "replace", "path": "/path/to/target", "value": <new value> }

Examples

Replace Primitive

Update a simple value

Patch:
{ "op": "replace", "path": "/age", "value": 30 }
Before:
{ "name": "John", "age": 25 }
After:
{ "name": "John", "age": 30 }

Replace Object

Replace an entire nested object

Patch:
{ "op": "replace", "path": "/address", "value": { "city": "LA" } }
Before:
{ "address": { "city": "NYC", "zip": "10001" } }
After:
{ "address": { "city": "LA" } }

Replace Array Element

Update a specific array item

Patch:
{ "op": "replace", "path": "/items/0", "value": "updated" }
Before:
{ "items": ["old", "b", "c"] }
After:
{ "items": ["updated", "b", "c"] }

Real World: Flip a Feature Flag

Switch a boolean setting in one operation

Patch:
{ "op": "replace", "path": "/features/beta", "value": true }
Before:
{ "features": { "beta": false, "dark_mode": true } }
After:
{ "features": { "beta": true, "dark_mode": true } }

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