
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeNo, the approach you suggested won't work for setting a specific array element by index. In Sanity's HTTP API patch operations, you cannot directly target array elements by numeric index in the set operation like that.
To modify a specific array element, you have a few options:
1. Use JSONMatch path syntax with the array index:
The correct way is to use the path notation in your set operation:
{
"patch": {
"id": "document-id",
"set": {
"data[34].field": "value"
}
}
}This targets the element at index 34 in the data array and sets its field property.
2. Target by a unique identifier (recommended):
If your array objects have a _key field (which they should for arrays of objects), it's better to target by that:
{
"patch": {
"id": "document-id",
"set": {
"data[_key==\"unique-key-here\"].field": "value"
}
}
}This is more reliable because array indices can change if items are added or removed, but _key values remain stable.
3. Replace the entire array element:
You can also replace the whole element at a specific index:
{
"patch": {
"id": "document-id",
"set": {
"data[34]": {
"_key": "some-key",
"field": "value",
"otherField": "otherValue"
}
}
}
}The key takeaway is that you use the path notation (like "data[34].field") in the keys of your set object, not as nested objects like in your example. The Sanity documentation on patches explains this uses JSONMatch syntax for targeting specific values in documents.
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store