How to merge multiple documents into a single flat object in GROQ?

10 replies
Last updated: Nov 29, 2025
I just need a little groq help. I've got three documents I'm querying from:
*[_type == "businessName" ||
   _type == "contact" ||
   _type == "address"]
which returns the following:

[
  {
    "_createdAt": "2021-10-21T00:22:05Z",
    "_id": "address",
    "_rev": "TrMJZl8kHDIulSDqHsmmGk",
    "_type": "address",
    "_updatedAt": "2021-10-21T03:25:59Z",
    "addressLine1": "123 Main Street",
    "city": "Sometown",
    "postal": "T8A 1A1",
    "province": "AB"
  },
  {
    "_createdAt": "2021-10-21T02:39:46Z",
    "_id": "businessName",
    "_rev": "TrMJZl8kHDIulSDqHvQf8Y",
    "_type": "businessName",
    "_updatedAt": "2021-10-21T12:30:55Z",
    "businessName": "Example Business Name"
  },
  {
    "_createdAt": "2021-10-21T00:21:00Z",
    "_id": "contact",
    "_rev": "qIVfBk0FCSxuep3bFCat6O",
    "_type": "contact",
    "_updatedAt": "2021-10-21T12:20:21Z",
    "email": "<mailto:email@domain.com|email@domain.com>",
    "fax": "<tel:780-555-4444|780-555-4444>",
    "phone": "<tel:780-555-5555|780-555-5555>"
  }
]
I'd like to be able to pull the relevant info from each returned object and put it into a flat object:

{
  "businessName": "Example Business Name",
  "email": "<mailto:email@domain.com|email@domain.com>",
  "fax": "<tel:780-555-4444|780-555-4444>",
  "phone": "<tel:780-555-5555|780-555-5555>"
  "addressLine1": "123 Main Street",
  "city": "Sometown",
  "postal": "T8A 1A1",
  "province": "AB"
}
but i'm not sure how to achieve this. Any assistance would be much appreciated!
AI Update

Great question! You want to merge multiple documents into a single flat object. In GROQ, you can achieve this using object spread syntax (...) combined with array indexing.

Here's the solution for your specific case:

{
  ...*[_type == "businessName"][0],
  ...*[_type == "contact"][0],
  ...*[_type == "address"][0]
}

This query:

  1. Fetches each document type separately using [0] to get the first (and only) document
  2. Uses the spread operator ... to unpack all fields from each document
  3. Combines them into a single flat object

The result will be exactly what you're looking for:

{
  "businessName": "Example Business Name",
  "email": "email@domain.com|email@domain.com",
  "fax": "780-555-4444|780-555-4444",
  "phone": "780-555-5555|780-555-5555",
  "addressLine1": "123 Main Street",
  "city": "Sometown",
  "postal": "T8A 1A1",
  "province": "AB"
}

Alternative approach if you want to be more explicit about which fields to include:

{
  "businessName": *[_type == "businessName"][0].businessName,
  "email": *[_type == "contact"][0].email,
  "fax": *[_type == "contact"][0].fax,
  "phone": *[_type == "contact"][0].phone,
  "addressLine1": *[_type == "address"][0].addressLine1,
  "city": *[_type == "address"][0].city,
  "postal": *[_type == "address"][0].postal,
  "province": *[_type == "address"][0].province
}

This second approach gives you more control over field naming and avoids accidentally including system fields like _id, _type, _createdAt, etc.

The spread operator approach is cleaner if you're confident about the field names not conflicting and you don't mind including all fields from each document!

Show original thread
10 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.

Was this answer helpful?