Build with Sanity MCP in Replit. Prizes up for grabs! Submit by June 14

Dereference only references in mixed array, leaving objects intact

3 repliesLast updated: Dec 1, 2025

if i have an array that includes both objects and references, is there any way to dereference (
->
) only the references, while leaving the other elements intact?
AI Update

Yes, you can selectively dereference only the references in a mixed array! The documented approach is to use conditional projections based on the _type field.

When you have an array containing both inline objects and references, reference objects have _type: "reference" while inline objects have their own _type values. You can use this to conditionally apply the dereference operator:

*[_type == "yourDocType"] {
  mixedArray[]{
    _type == "reference" => @->,
    @
  }
}

This pattern uses object-level conditional projections where:

You can also use the select() function for more explicit conditional logic:

*[_type == "yourDocType"] {
  mixedArray[]{
    select(
      _type == "reference" => @->,
      @
    )
  }
}

Both approaches check the _type field to determine whether to dereference. This is particularly useful for page builder arrays or any field that accepts both inline content blocks and references to other documents.

Why this works: References in Sanity are stored as objects with _type: "reference" and a _ref field containing the document ID. The reference access operator (->) only resolves these reference objects, so by checking _type == "reference" first, you ensure dereferencing only happens when appropriate.

Show original thread
3 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