How to create a field with all the artists of the artworks in a nested array in a GROQ request.
Based on your GROQ query, it looks like you're working with nested arrays of references - specifically dereferencing artworks and then dereferencing artists within each artwork. Your syntax looks correct! Let me explain what's happening and address any potential considerations.
Your query structure:
"artworks": artworks[]->{..., "artists":artists[]->{name},"image":image.asset->url}This is doing several things:
artworks[]->- dereferencing each item in the artworks array- Inside each artwork,
"artists":artists[]->- dereferencing each artist reference within that artwork "image":image.asset->url- dereferencing the image asset
This nested dereferencing pattern is perfectly valid in GROQ. The reference access operator (->) works with arrays and can be nested as you're doing.
A few things to keep in mind:
Performance: Each -> operator performs an internal subquery, so deeply nested references can impact query performance. With artworks[]-> and then artists[]-> inside, you're potentially making many subqueries if you have lots of artworks with multiple artists each.
Syntax variation: You could also write the artists dereferencing in a slightly different way:
"artists": artists[]->{name}
// or if you want just the names as a flat array:
"artists": artists[]->nameThe second form (artists[]->name) gives you a flat array of names directly, while your current syntax (artists[]->{name}) gives you an array of objects each with a name property.
Common issues to check:
- Make sure your references are properly defined in your schema with
type: 'reference' - Verify that the referenced documents actually exist in your dataset
- Check that
_keyproperties exist on array items if you're updating them (required for objects in arrays)
Your GROQ syntax looks correct, so if you're experiencing specific issues, they're likely related to the data structure or schema configuration rather than the query syntax itself. What specific problem are you running into?
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.