JSON Patch Examples

Real-world JSON Patch scenarios. Load any example into the tool, then generate the patch, apply it to the source, or verify it round-trips.

Add a field to a JSON object

The add operation inserts a value where none existed - the parent path must exist, the final key must not.

Add a New Field

Objects

Creating a property that did not exist with one add operation.

Source JSON

{
  "name": "John",
  "age": 25
}

Target JSON

{
  "name": "John",
  "age": 25,
  "email": "john@example.com"
}

Load this example, then pick a mode

Learn more: The add operation

Rename, move and copy values

Renaming a key is a move: relocate the value and the old key disappears. Copy duplicates a value without touching the source.

Rename a Key

Objects

The move operation removes a value from one path and adds it at another as a single operation.

Source JSON

{
  "username": "jdoe",
  "active": true
}

Target JSON

{
  "handle": "jdoe",
  "active": true
}

Load this example, then pick a mode

Learn more: The move operation

Duplicate a Value

Objects

The copy operation clones a value to a second location.

Source JSON

{
  "billing": {
    "address": {
      "city": "NYC",
      "zip": "10001"
    }
  }
}

Target JSON

{
  "billing": {
    "address": {
      "city": "NYC",
      "zip": "10001"
    }
  },
  "shipping": {
    "address": {
      "city": "NYC",
      "zip": "10001"
    }
  }
}

Load this example, then pick a mode

Learn more: The copy operation

Update values in a JSON object

The everyday case: change fields in place with replace, and add the ones that are missing.

User Profile Update

Common

Updating user information with new email and preferences.

Source JSON

{
  "id": 123,
  "name": "John Doe",
  "email": "john@example.com",
  "age": 25,
  "preferences": {
    "theme": "light",
    "notifications": true
  }
}

Target JSON

{
  "id": 123,
  "name": "John Smith",
  "email": "john.smith@example.com",
  "age": 26,
  "preferences": {
    "theme": "dark",
    "notifications": true,
    "language": "en"
  }
}

Load this example, then pick a mode

Learn more: The replace operation

Shopping Cart Operations

E-commerce

Adding items and applying discounts to a shopping cart.

Source JSON

{
  "items": [
    {
      "id": "item1",
      "name": "Laptop",
      "price": 999
    },
    {
      "id": "item2",
      "name": "Mouse",
      "price": 25
    }
  ],
  "total": 1024,
  "discount": 0
}

Target JSON

{
  "items": [
    {
      "id": "item1",
      "name": "Laptop",
      "price": 999
    },
    {
      "id": "item2",
      "name": "Mouse",
      "price": 25
    },
    {
      "id": "item3",
      "name": "Keyboard",
      "price": 75
    }
  ],
  "total": 1099,
  "discount": 10
}

Load this example, then pick a mode

Learn more: The add operation

Configuration Management

DevOps

Updating application configuration for production deployment.

Source JSON

{
  "database": {
    "host": "localhost",
    "port": 5432,
    "ssl": false
  },
  "cache": {
    "enabled": true,
    "ttl": 3600
  },
  "features": [
    "auth",
    "logging"
  ]
}

Target JSON

{
  "database": {
    "host": "prod-db.company.com",
    "port": 5432,
    "ssl": true,
    "pool_size": 20
  },
  "cache": {
    "enabled": true,
    "ttl": 7200
  },
  "features": [
    "auth",
    "logging",
    "analytics"
  ]
}

Load this example, then pick a mode

Learn more: The add operation

JSON Patch array operations

Arrays are where most patches go wrong: indexes shift as you edit, and the /- path appends without knowing the length.

Append to an Array

Arrays

Adding an item to the end of an array with the /- path.

Source JSON

{
  "tags": [
    "a",
    "b"
  ]
}

Target JSON

{
  "tags": [
    "a",
    "b",
    "c"
  ]
}

Load this example, then pick a mode

Learn more: JSON Patch and arrays

Remove an Array Element

Arrays

Deleting by index - later elements shift one position left.

Source JSON

{
  "queue": [
    "first",
    "second",
    "third"
  ]
}

Target JSON

{
  "queue": [
    "first",
    "third"
  ]
}

Load this example, then pick a mode

Learn more: JSON Patch and arrays

Update nested JSON properties

Deep values are patched with the full JSON Pointer path - one operation per value, no need to resend whole objects.

Update a Nested Property

Nested

Changing one deep value with the full JSON Pointer path.

Source JSON

{
  "user": {
    "name": "Ana",
    "address": {
      "city": "NYC",
      "zip": "10001"
    }
  }
}

Target JSON

{
  "user": {
    "name": "Ana",
    "address": {
      "city": "Boston",
      "zip": "10001"
    }
  }
}

Escape special characters in JSON Pointer paths

Keys that contain / or ~ still work: RFC 6901 escapes them as ~1 and ~0.

Escaped JSON Pointer

Pointer

Patching a key that literally contains a slash - /a~1b.

Source JSON

{
  "a/b": {
    "timeout": 30
  }
}

Target JSON

{
  "a/b": {
    "timeout": 60
  }
}

Load this example, then pick a mode

Learn more: JSON Pointer escaping

Test a value before changing it

The test operation adds a precondition: if the value does not match, evaluation stops at that operation.

Test Before You Write

Safety

Stopping evaluation when the document is not in the expected state.

Source JSON

{
  "version": 1,
  "status": "draft"
}

Target JSON

{
  "version": 2,
  "status": "published"
}

Load this example, then pick a mode

Learn more: The test operation

Guarded Config Rollout

DevOps

Flip a production flag only if it is still off - test first, then change.

Source JSON

{
  "database": {
    "ssl": false,
    "pool_size": 10
  }
}

Target JSON

{
  "database": {
    "ssl": true,
    "pool_size": 20
  }
}

Load this example, then pick a mode

Learn more: The test operation

Using JSON Patch in Your Code

Get started with JSON Patch in JavaScript or Python in just a few lines.

JavaScript
Python
import { compare, applyPatch } from 'fast-json-patch'; // ESM
// Generate patch
const patch = compare(oldObj, newObj);
// Apply patch
const result = applyPatch(oldObj, patch).newDocument;

Ready to create your own patches?

Generate, apply, and verify JSON Patch operations - free, right in your browser.

Open JSON Patch Generator