How to query single object by ID without array response in Sanity?
Based on the Sanity documentation, yes, there's a simple way to get a single object instead of an array!
Use Array Index [0]
When you add [0] to your query, GROQ returns a single object instead of an array:
*[_type == "movie" && _id == myID][0]This returns an object (not an array with one item).
How It Works
The slice operation [0] extracts the first element from the result array. Since you're filtering by _id (which is unique), you'll only have one result anyway, so [0] effectively unwraps it from the array.
Examples
// Returns an array with one movie
*[_type == "movie" && title == "Alien"]
// Returns a single movie object
*[_type == "movie" && title == "Alien"][0]
// Your specific case - returns single object
*[_type == "movie" && _id == myID][0]Important Note
If no document matches your filter, [0] will return null rather than an empty array []. This is actually useful for checking if a document exists:
const movie = await client.fetch('*[_id == $id][0]', { id: myID })
if (!movie) {
// Document doesn't exist
}This is the standard pattern in GROQ for fetching a single document by ID or any unique identifier!
Show original thread10 replies
Sanity – Build the way you think, not the way your CMS thinks
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.