GROQ: Access nested array without intermediate object structure
Looking at your query, the issue is with how you're structuring your projections with the reference operator. When you write:
'selected': selected->{
type[0]{
selected[]->{...}
}
}You're creating nested objects unnecessarily. The reference operator -> already resolves the reference, and then you're trying to traverse into it with additional projections that create the unwanted structure.
To fix this and get just the selected array directly, you need to flatten your projection. Here are a few solutions depending on what you're trying to achieve:
If selected is a reference to a document that has a type array, and you want the first type's selected array:
'selected': selected->type[0].selected[]->This chains the operations: dereference selected, access the first item in type, then get the selected array and dereference each item.
If you want to project specific fields from the dereferenced items:
'selected': selected->type[0].selected[]->{
_id,
name
// your specific fields
}If selected itself is an array of references:
'selected': selected[]->{
// fields you want
}The key insight from the reference access operator documentation is that -> resolves the reference and returns the document data. Each time you wrap something in { }, you're creating a new object level in your response.
Your current query creates this structure because of the nested object projections:
selected->creates the first leveltype[0]{ }creates the second levelselected[]->creates the third level
Remove the intermediate object wrapping (type[0]{ }) and access the path directly with dot notation or array access. The correct pattern for array references is to use []-> directly on the array field without wrapping it in projection objects.
Show original thread2 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.