How to dereference objects inside Sanity array fields (pageBuilder)?
The issue you're encountering is that when you have an array with multiple types (like content and a reference), GROQ doesn't automatically know which items in the array to dereference. You're trying to dereference ctaSection at the root level, but ctaSection is actually inside your pageBuilder array, not a field on the document itself.
Here's what you need to do - dereference the items within the array:
*[_type == 'insurance' && slug.current == $slug][0]{
...,
pageBuilder[]{
...,
_type == "ctaSection" => @->{
picture,
title,
cta{
label,
reference->{
_type,
"slug": "/" + slug.current,
}
}
}
}
}The key changes:
pageBuilder[]- Project over the array items_type == "ctaSection" => @->{...}- Conditionally dereference only when the item type isctaSection. The@represents the current item in the array being processed...- Spread operator to preserve the original fields for non-matching items
This preserves other array items (like your content type) while dereferencing only the ctaSection references.
Alternatively, if you want to dereference all references in the array regardless of their name, you can check for the reference type:
pageBuilder[]{
...,
_type == "reference" => @->{
picture,
title,
cta{
label,
reference->{
_type,
"slug": "/" + slug.current,
}
}
}
}The reason you're only seeing {_key, _ref, _type} is because your original query ctaSection-> is looking for a field literally named ctaSection on the insurance document itself, but that field doesn't exist at the root level - it's a named reference type inside your array.
Show original thread5 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.