GROQ: Dereferencing nested image reference (reference in a reference)
Looking at your query, you're dealing with a nested reference situation where icon.asset is a reference that needs to be dereferenced. The issue is that you need to dereference the asset reference within the context of the icon object.
Based on the data structure you showed, where icon.asset contains {_ref: "image-...", _type: "reference"}, you need to properly dereference that asset reference. The GROQ reference access operator -> can be chained to handle nested references.
Try this approach to get the full asset document:
"footerNavSocial": footerNavSocial->social[]{
"alt": icon.alt,
"image": icon.asset->,
link,
name,
}If that's still returning empty, the path might need adjustment. Since you're working with Sanity image assets, you typically want to access the url field from the dereferenced asset:
"footerNavSocial": footerNavSocial->social[]{
"alt": icon.alt,
"imageUrl": icon.asset->url,
link,
name,
}Or to get multiple fields from the asset:
"footerNavSocial": footerNavSocial->social[]{
"alt": icon.alt,
"image": icon.asset->{url, metadata},
link,
name,
}If you're still getting empty objects, it could mean:
- The reference path isn't quite right - double-check that
icon.asset._refstructure in your schema - The asset documents don't exist or have been deleted
- There might be an issue with how
iconis structured - it might itself be a reference that needs dereferencing first
If icon is also a reference, you'd need to dereference it before accessing asset:
"footerNavSocial": footerNavSocial->social[]{
"alt": icon->alt,
"image": icon->asset->url,
link,
name,
}The key insight from the reference operator documentation is that each -> internally executes a subquery to fetch the referenced document, and you can chain these to traverse multiple levels of references.
Show original thread8 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.