> 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).

# JSONMatch

Rich ways to address specific values in Sanity documents

JSONMatch is the path syntax Content Lake uses to address one or more values in a JSON document. It's widely used in the [patch](https://www.sanity.io/docs/content-lake/http-patches) mutation type when updating documents. All patch operations accept JSONMatch paths as keys. This means that a single `set`, `unset`, `insert`, or `inc` operation can target one or more values of the document, or use the recursive filtering of JSONMatch to find the desired value of the document automatically.

## General format

A JSONMatch path is an expression that, when evaluated, resolves to one or more locations in a JSON document. A path can traverse object keys and arrays.

## Examples

The examples in this reference use the following JSON document:

```json
{
  "name": "fred",
  "friends": [
    {
      "name": "mork",
      "age": 40,
      "favoriteColor": "red"
    },
    {
      "name": "mindy",
      "age": 32,
      "favoriteColor": "blue"
    },
    {
      "name": "franklin",
      "favoriteColor": "yellow"
    }
  ],
  "roles": ["admin", "owner"],
  "contactInfo": {
    "streetAddress": "42 Mountain Road",
    "state": {
      "shortName": "WY",
      "longName": "Wyoming"
    }
  }
}

```

Given the example document, these expressions can be evaluated:

```text
"name" → "fred"
"friends[*].name" → ["mork", "mindy", "franklin"]
"friends[age > 35].name" → ["mork"]
"friends[age > 30, favoriteColor == "blue"].name" → ["mork", "mindy"]
"friends[age?].age" → [40, 32]
"friends[0].name" → "mork"
"friends[0, 1].name" → ["mork", "mindy"]
"friends[1:2].name" → ["mindy"]
"friends[0, 1:2].name" → ["mork", "mindy"]
"contactInfo.state.shortName" → "WY"
"contactInfo.state[shortName, longName]" → ["WY", "Wyoming"]
"roles[@ == "admin"]" → ["admin"]
"roles" → ["admin", "owner"]
"roles[*]" → ["admin", "owner"]
"roles[0]" → "admin"
"roles[-1]" → "owner"
"contactInfo..shortName" → "WY"
"[contactInfo.state.shortName, roles]" → ["WY", ["admin", "owner"]]

```

## Keys

A single key matches that key in an object. For example, `name` returns `"fred"`. If [keys](https://www.sanity.io/docs/content-lake/ids) contain special characters the key name can be surrounded in single quotes, so `'name'` also returns `"fred"`.

> [!WARNING]
> Use double quotes for strings
> Because single quotes are used to denote key names, regular strings *must* be enclosed in double quotes.

## Descent operator

The `.` operator descends into a key and selects a nested key. It has the format: `key1.key2`

For example:

```text
friend.name
```

This matches the `name` key in:

```json
{
  "friend": {
    "name": "mork"
  }
}

```

## Recursive descent

The `..` operator matches every value below the current selection descending through any objects, iterating over every array. Typical usage is to find a sub-object regardless of where it resides in an object. `content.blocks..[_key == "abc123"]` finds the object whose `_key` field equals "abc123", wherever it resides inside the object or array at `content.blocks`.

## Arrays

Arrays can be subscripted with the `[]` operator. It has the formats:

```text
"array[2]" → the element at index 2 (indexes start at 0, so this is the third element)
"array[2, 3, 9]" → the elements at indexes 2, 3, and 9
"array[-1]" → the last array element
"array[1:9]" → array element 1 through 9 (non-inclusive)
"array[4:]" → array element 4 through to the end of the array
"array[:4]" → array elements from the start to element 4 of the array (non-inclusive)
"array[1, 4, 5:9, 12]" → union of array elements 1, 4, 5 through 8, and 12

```

## Wildcards

The `*` wildcard selects every value in an object or every element in an array. For example, `contactInfo.state.*` selects both `shortName` and `longName`, and `roles[*]` selects every element of the `roles` array.

## Constraints

Arrays can be filtered with constraints, for example, `friends[age == 32]`. Constraints are separated by commas and are always a union ("or"), not an intersection.

Constraints support these comparison operators:

- `==` and `!=`: equal to and not equal to
- `>` and `>=`: greater than and greater than or equal to
- `<` and `<=`: less than and less than or equal to
- `?` after a key: existence check, for example `friends[age?]`

The right-hand side of a comparison can be a literal or another key, for example `products[price > salePrice]`. JSONMatch also accepts the JSONPath filter syntax `[?(...)]`, so `friends[?(age > 35)].name` matches the same values as `friends[age > 35].name`.

## Boolean operations

In its current implementation, JSONMatch does not support [boolean operators](https://www.sanity.io/docs/specifications/groq-operators) `&&` or `||`. However, a union is the same as boolean `or`, and chaining constraints works the same as boolean `and`:

`"numbers[@ < 50, @ > 60]"`: Select numbers that are < 50 OR > 60.

`"numbers[@ > 20][@ < 30]"`: Select numbers that are > 20 AND < 30.

`'employees[name == "John Smith", name == "Granny Smith"]'`: employees that have the name "John Smith" OR "Granny Smith".

## Path syntax errors

If Content Lake can't parse a path, the error message describes the problem. These two messages are common:

- `Wrap numbers in brackets when used in dotted path expressions ([0] or ["0"] depending on what you mean)`: the path ends with a number after a dot, such as `friends.0`. Use `friends[0]` for an array index, or `friends['0']` for an object key named "0".
- `Syntax error. (Illegal token "0.")`: a number followed by a dot in the middle of a path, such as `friends.0.name`. Use `friends[0].name` instead.

