
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeUnfortunately, GROQ doesn't support dynamic keys where you can use a field's value as the key name in the returned object. In your example, you're trying to return an object where each document is keyed by its navId.current value, but GROQ doesn't have this capability built-in.
What GROQ can do:
{"id": navId.current}*[_type == 'navigation']{...}What GROQ cannot do:
{ "nav-1": {...}, "nav-2": {...} })Workarounds:
*[_type == 'navigation'] {
navId,
"id": navId.current,
items[]{ /* ... */ }
}Then in JavaScript:
const result = await client.fetch(query);
const keyed = result.reduce((acc, item) => {
acc[item.navId.current] = item;
return acc;
}, {});Use array format: Keep your data as an array and use array methods like .find() when you need to access specific items by their navId.
Custom GROQ functions: If you're on a plan that supports custom GROQ functions, you might be able to create a custom function for this transformation, though this is a more advanced approach.
The client-side transformation is the standard approach for this use case since it's simple, performant, and gives you the flexibility to structure your data however you need it in your application. This is a common pattern when working with GROQ - the query language is designed to return arrays of documents, and you handle reshaping into other data structures in your application code.
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