The Sanity engineering blog is live. Deep dives from our engineers.

How to dereference objects inside Sanity array fields (pageBuilder)?

5 repliesLast updated: Nov 29, 2025

What am I doing wrong here?
{
      name: "pageBuilder",
      type: "array",
      title: "Page content",
      of: [
        { type: "content" },
        {
          type: "reference",
          name: "ctaSection",
          title: "CTA",
          to: [{ type: "ctaBlocks" }],
        },
      ],
    },

*[_type == 'insurance' && slug.current == $slug][0]{
    ...,
    ctaSection->{
      picture,
      title,
      cta{
        label,
        reference->{
          _type,
          "slug": "/" + slug.current,
        }
      }
    }
  }
This is all im getting

{
      _key: '6569f1186d08',
      _ref: 'c92ccb9a-5441-4fca-8dea-8fb7cbbb7cfd',
      _type: 'ctaSection'
    }
AI Update

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:

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 thread
5 replies

Was this answer helpful?

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.

Related contributions