json patch examples

JSON Patch Examples: Mastering JSON Patching

By David Rodriguez
AD

JSON Patch, also known as JSON Merge Patch or JSON Patch (RFC 6902), is a standard for applying partial modifications to a resource. It's widely used in REST APIs to update specific fields of a resource without sending the entire updated object. Let's dive into JSON Patch examples to understand its usage and benefits.

Understanding JSON Patch

JSON Patch is a simple format that describes a sequence of operations to apply to a resource. It consists of an array of operations, where each operation is a JSON object with a "op" property indicating the type of operation (add, remove, replace, move, copy, or test), and other properties depending on the operation type.

JSON Patch Operations

Here are the basic operations defined in JSON Patch:

AD
Dea Badge, New York Transit Police Patch, Erie County Swat Patch, Nypd Detective Patch, Los Angeles Police Investigation Patch, Nyc Transit Police Academy Patch, New York Police Csi Patch, Nevaada Police Patches Collection, New York Mta Police Patch
Dea Badge, New York Transit Police Patch, Erie County Swat Patch, Nypd Detective Patch, Los Angeles Police Investigation Patch, Nyc Transit Police Academy Patch, New York Police Csi Patch, Nevaada Police Patches Collection, New York Mta Police Patch
a black and green patch that says you're dry dumping my last nerve
a black and green patch that says you're dry dumping my last nerve
a poster with many different stickers on it
a poster with many different stickers on it
  • add: Adds a new value or object to the resource.
  • remove: Removes a value or object from the resource.
  • replace: Replaces an existing value or object in the resource.
  • move: Moves a value or object within the resource.
  • copy: Copies a value or object within the resource.
  • test: Tests whether a value or object exists in the resource.

Path Expressions

JSON Patch uses JSON Pointer (RFC 6901) for path expressions to specify the location of the value or object to be modified. Path expressions start with a slash (/) and use simple key names to navigate through the JSON object.

JSON Patch Examples

Example 1: Updating a User's Email

Let's say we have the following user object:

three different types of patch with the text how to choose the right patch embroidery vs pvc's woven
three different types of patch with the text how to choose the right patch embroidery vs pvc's woven
several patches with different types of words and symbols on them, all in different colors
several patches with different types of words and symbols on them, all in different colors
many different colored patches are on a red surface with the words oregon, oregon, and chicago written below them
many different colored patches are on a red surface with the words oregon, oregon, and chicago written below them
{
  "name": "John Doe",
  "email": "johndoe@example.com",
  "address": {
    "street": "123 Main St",
    "city": "Anytown"
  }
}

To update the user's email, we can use the following JSON Patch:

[
  {
    "op": "replace",
    "path": "/email",
    "value": "johndoe_new@example.com"
  }
]

Example 2: Adding a New Address

To add a new address to the user, we can use the following JSON Patch:

AD
[
  {
    "op": "add",
    "path": "/address/2",
    "value": {
      "street": "456 Oak Ave",
      "city": "Othertown"
    }
  }
]

Example 3: Removing an Address

To remove the first address, we can use the following JSON Patch:

various patches and stickers on a black surface
various patches and stickers on a black surface
many different patches and stickers on a striped background
many different patches and stickers on a striped background
1 Set Of 3/8pcs Love Pentagram American Flag English Letter Embroidery Patches Cute Cartoon Polyester Badge Applique DIY Clothing Iron On Patches Sew Accessory Cloth Stickers Suitable For Backpacks, Jeans, Jackets, T-Shirts, Hats, Dresses, Etc
1 Set Of 3/8pcs Love Pentagram American Flag English Letter Embroidery Patches Cute Cartoon Polyester Badge Applique DIY Clothing Iron On Patches Sew Accessory Cloth Stickers Suitable For Backpacks, Jeans, Jackets, T-Shirts, Hats, Dresses, Etc
Embroidery Patches/Iron on Patches
Embroidery Patches/Iron on Patches
an image of many different patches and badges
an image of many different patches and badges
JTHM Question Sleep Z? Logo Patch
JTHM Question Sleep Z? Logo Patch
[
  {
    "op": "remove",
    "path": "/address/0"
  }
]

Example 4: Moving an Address

To move the second address to the first position, we can use the following JSON Patch:

[
  {
    "op": "move",
    "from": "/address/1",
    "path": "/address/0"
  }
]

Benefits of Using JSON Patch

JSON Patch offers several benefits, including:

AD
  • Reduced bandwidth usage by only sending the changes instead of the entire resource.
  • Simplified client-side code by handling complex updates on the server.
  • Improved security by reducing the exposure of sensitive data.
  • Better support for partial updates and optimistic concurrency control.

JSON Patch is a powerful tool for updating resources in a REST API. By understanding and leveraging its operations and path expressions, developers can create more efficient and secure APIs. Whether you're updating user profiles, managing inventory, or handling any other resource, JSON Patch can help streamline your update processes.

How to Sew a Patch, The Correct Way by Hand or Machine TREASURIE
How to Sew a Patch, The Correct Way by Hand or Machine TREASURIE
a black and green patch with the words certified zombie hunter on it's side
a black and green patch with the words certified zombie hunter on it's side
Embroidered Patch
Embroidered Patch
an orange and white patch sitting on top of a forest floor next to a tree
an orange and white patch sitting on top of a forest floor next to a tree
the special activities division logo on a black and gold patch
the special activities division logo on a black and gold patch
Caution embroidered iron on patch
Caution embroidered iron on patch
This item is unavailable - Etsy
This item is unavailable - Etsy
Ask, Trust, Thank God Embroidered Patch Religious Iron on Patch
Ask, Trust, Thank God Embroidered Patch Religious Iron on Patch
God is Within Her She Will Not Fall Patch, Coquette Bow Christian Faith Bible Patch Applique for Hats Backpacks Jackets Gifts for Her Him
God is Within Her She Will Not Fall Patch, Coquette Bow Christian Faith Bible Patch Applique for Hats Backpacks Jackets Gifts for Her Him
many different patches and stickers on the back of a blue jean jacket
many different patches and stickers on the back of a blue jean jacket
an embroidered patch with wings and the letter a on it's back side, in black and red
an embroidered patch with wings and the letter a on it's back side, in black and red
Patch Outfits Is The Upgrade Your Wardrobe Needs
Patch Outfits Is The Upgrade Your Wardrobe Needs
AD