> For AI agents: the complete Sanity documentation index is available at [https://www.sanity.io/docs/llms.txt](https://www.sanity.io/docs/llms.txt).

# Patches

The valid patch types when using the direct HTTP Mutations API.

It is good practice to use **patches** when modifying Sanity documents programmatically instead of replacing entire documents. Patches should make the smallest, most specific change possible so that if multiple scripts or users are modifying the same documents at the same time, Sanity is able to merge those changes in a sensible way.

A patch is a special mutation you can use with the [Mutations API](https://www.sanity.io/docs/http-reference/mutation), some actions in the [Actions API](https://www.sanity.io/docs/http-reference/actions), and in [migrations](https://www.sanity.io/docs/content-lake/schema-and-content-migrations). Because these endpoints are transactional, you can submit one or several patches at once, changing any number of documents in a single transaction. The following example is a full transaction that submits two patches at once. It sets the name property of the document with the ID "person-1234" to "Remington Steele", and adds a reference to that document to the end of the people array of the document with the ID "remingtons":

```json
{
  "mutations": [
    {
      "patch": {
        "id": "person-1234",
        "set": {
          "name": "Remington Steele"
        }
      }
    },
    {
      "patch": {
        "id": "remingtons",
        "insert": {
          "after": "people[-1]",
          "items": [
            {
              "_type": "reference",
              "_ref": "person-1234"
            }
          ]
        }
      }
    }
  ]
}
```

The keys in a patch generally use [JSONMatch syntax](https://www.sanity.io/docs/content-lake/json-match) to target values for change. This syntax generally allows for paths like `some.array[8].attribute`, but can also do pattern matching, recursive search, and target multiple values at once.

> [!TIP]
> Protip
> `JSONMatch` is a variant of `JSONPath` that simplifies the syntax and minimizes the number of special characters required to express a path.

## Field name restrictions

When using patch operations, [field names](https://www.sanity.io/docs/apis-and-sdks/naming-things) that start with digits must use bracket notation. For example, if you have a field named `123field` or a UUID like `37819f29-cd8e-438a-bf53-27953351677a`, you cannot use:

```json
{
    "set": {
        "123field": "value",
        "37819f29-cd8e-438a-bf53-27953351677a": "value"
    }
}
```

Instead, use bracket notation:

```json
{
    "set": {
        "['123field']": "value",
        "['37819f29-cd8e-438a-bf53-27953351677a']": "value"
    }
}
```

This applies to all patch operations, including `set`, `setIfMissing`, `inc`, and `dec`. If you use a field name that starts with a digit without bracket notation, the API returns an error like this one: `field name '123field' starts with a digit, which is not valid in JSONPath. Use bracket notation instead: ['123field']`.

> [!NOTE]
> Validation added in API version 2025-08-18
> This validation was introduced in API version 2025-08-18. Earlier versions may silently fail or return unclear errors when using numeric field names.

## Patch types

### set

`set` performs a shallow merge of its argument into the document. Each key in the argument is either an attribute or a JSON path.

#### Usage

```json
{
  "set": {
    attributeOrJSONPathExpression: any
  }
}
```

#### Examples

**Object properties**

Set the field `name` to the value `Bob` and the nested field `personalMetrics.height` to `201`:

```json
{
  "set": {
    "name": "Bob",
    "personalMetrics.height": 201
  }
}
```

**Arrays**

Set the `text` property to the value `Do the thing!` in all objects in the `body` array where the `_type` is `cta`:

```json
{ 
  "set": {
    "body[_type==\"cta\"].text": "Do the thing!" 
  }
}
```

> [!WARNING]
> Gotcha
> Notice that the array filter (`[_type == \"cta\"]`) must use double quotes. If you are in JSON, you must [escape them](https://stackoverflow.com/a/15637481/1285253) (`\"`). 

### setIfMissing

`setIfMissing` is like `set`, except existing keys are preserved and not overwritten.

### unset

Deletes one or more attributes. Each entry in the argument is either an attribute or a JSON path. Missing attributes are ignored. Unset can also be used to delete elements of an array.

#### Usage

```json
{
  "unset": [ attributeOrJSONPathExpression, ... ] 
}
```

#### Example

```json
{ 
  "unset": ["subtitle", "legacyId"] 
}
```

### insert

`insert` modifies arrays by inserting, appending, or replacing elements using a JSONMatch path.

#### Append to the end of an array

Inserts the string `"a"` at the end of the array `tags`:

```json
{
  "insert": {
    "after": "tags[-1]",
    "items": ["a"]
  }
}
```

#### Insert into an array

Inserts the string `"a"` at index 2 before whatever was there:

```json
{
  "insert": {
    "before": "tags[2]",
    "items": ["a"]
  }
}
```

#### Prepend to the start of an array

This inserts the string `"a"` at the beginning of the array:

```json
{
  "insert": {
    "before": "tags[0]",
    "items": ["a"]
  }
}
```

#### Replace items in an array

This removes index 2 through the end of the array, replacing the content with `"a"`:

```json
{
  "insert": {
    "replace": "tags[2:]",
    "items": ["a"]
  }
}
```

#### Advanced use of JSONMatch

For more details, see the [article on JSONMatch](https://www.sanity.io/docs/content-lake/json-match).

Finds the element that has `key == "abc-123"` and inserts `"a"` after it:

```json
{
  "insert": {
    "after": "body[key == \"abc-123\"]",
    "items": ["a"]
  }
}
```

Finds any object that has `key == "list-123"` and adds `"a"` at the beginning of its items array:

```json
{
  "insert": {
    "before": "body..[key==\"list-123\"].items[0]",
    "items": ["a"]
  }
}
```

> [!WARNING]
> Gotcha
> Because single quotes denote field names, regular strings *must* be enclosed in double quotes. When defining patches in JSON, the double quotation marks need to be [escaped](https://stackoverflow.com/a/15637481/1285253) (`\"`).

### inc/dec

`inc` and `dec` change a numeric value. Each entry in the argument is either an attribute or a JSON path. For each entry, the attribute is looked up, modified, and stored. The value can be a positive or negative integer or floating-point value. The operation fails if the target value isn't numeric or doesn't exist.

`inc` increments; `dec` is the same as `inc`, except the value is decremented.

#### Examples

```json
{
  "inc": {
    "stats.visitorCount": 1
  }
}
```

If you're not sure whether the attribute exists, provide a default with `setIfMissing`:

```json
{
  "setIfMissing": {
    "stats.visitorCount": 0
  },
  "inc": {
    "stats.visitorCount": 1
  }
}
```

### diffMatchPatch

This operation supports robust incremental text patches according to the [Google diff-match-patch algorithm](https://github.com/google/diff-match-patch), which has wide library support in practically all programming languages in common use. Given the document:

```json
{
  "_id": "dog-1",
  "_type": "someType",
  "aboutADog": "The rabid dog"
}
```

The following patch applies a diff-match-patch patch to the string:

```json
{
  "patch": {
    "id": "dog-1",
    "diffMatchPatch": {
      "aboutADog": "@@ -1,13 +1,12 @@\n The \n-rabid\n+nice\n  dog\n"
    }
  }
}
```

The document is transformed to read:

```json
{
  "_id": "dog-1",
  "_type": "someType",
  "aboutADog": "The nice dog"
}
```

The beauty of diff-match-patch patches is that they allow you to modify huge strings with small patches, and that they compose well, generally giving sane results even when several users or scripts are modifying the same text.



## Related changelog entries

Entries are listed newest first; each link points to the full entry as markdown. Follow one when you need to know what changed, when, or why — for example, to summarize recent updates, explain behavior that differs from older documentation, or check whether a fix has shipped.

- [Content Lake v2025-08-18: Numeric Field Name Validation in Patches](https://www.sanity.io/docs/changelog/76bf5f16-3216-4ec9-9b12-8e91892f5b38.md) — August 18, 2025