Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

How to destructure data for an array of references inside a fieldset in Sanity.

4 replies
Last updated: Mar 3, 2021
Is there a way to return destructured data for an array of references when inside of a fieldset?
I'm using the tabs plugin on Sanity, and I have an array of references inside one of the tabs. However, I'm having a hard time getting past the first content object to destructure the array.

Here is the data structure:

{
  "_createdAt": "2021-01-23T18:23:03Z",
  "_id": "eb2ea4ad-b9dd-48d2-a12f-d624072c3f85",
  "_rev": "kb0HY6NIXzvB0Lryh3yfgp",
  "_type": "stylist",
  "_updatedAt": "2021-01-27T02:19:29Z",
  "content": {
    "_type": "object",
    "businessName": "...",
    "emailAddress": "...",
    "hours": [
      {
        "_key": "6ec087d3b1b8",
        "day": "Mon",
        "hoursValue": "10:00am - 8:00pm"
      },
      {
        "_key": "cebf173f6c30",
        "day": "Wed",
        "hoursValue": "10:00am - 8:00pm"
      },
      {
        "_key": "e28b97392ccc",
        "day": "Fri",
        "hoursValue": "10:00am - 6:00pm"
      },
      {
        "_key": "4e22af69c6b8",
        "day": "Sat",
        "hoursValue": "By appointment only"
      }
    ],
    "phoneNumber": "3035505676",
    "services": [
      {
        "_key": "930fef6cff22",
        "_ref": "b761952b-5280-4abc-b1cf-8f126b0b2717",
        "_type": "reference"
      },
      {
        "_key": "0474ffdda5dc",
        "_ref": "48dc64ec-04e1-45e0-a035-356a96c5d519",
        "_type": "reference"
      },
      {
        "_key": "dd7f83a6a1fc",
        "_ref": "8000399d-8782-4801-ba0b-13fbd3eabeaf",
        "_type": "reference"
      },
      {
        "_key": "e6943b847829",
        "_ref": "ac00790b-694f-4903-9c89-44a78d001d2d",
        "_type": "reference"
      }
    ],
    "suiteNumber": 202
  },
  "location": {
    "_ref": "eee608e6-47ce-4397-ad26-06fab5a329de",
    "_type": "reference"
  },
  "name": "...",
  "slug": {
    "_type": "slug",
    "current": "...-..."
  }
}
You can see the "Services" array is inside of the "Content" object, but I am unable to run something like the code block to be able to destructure the Services array like I need to in order to return the name of the service.


*[_type == "stylist" && name == "Alex Clark"] {
  name,
  content->services[]->
}
Mar 2, 2021, 10:53 PM
Hi Austin. What about:

*[_type == "stylist" && name == "Alex Clark"] {
  name,
  "services": content.services[]->
}
or


*[_type == "stylist" && name == "Alex Clark"] {
  name,
  content {
    services[]->
  }
}

Mar 2, 2021, 11:38 PM
Hi Geoff! These could be the golden ticket! Let me try them out šŸ™‚
Mar 2, 2021, 11:56 PM
You're the best! Worked like a charm šŸ‘. I opted for this structure:

*[_type == "stylist" && slug.current == $slug] {
    name,
    slug,
    content {
      bio,
      website,
      phoneNumber,
      emailAddress,
      hours,
      facebookUsername,
      instagramUsername,
      services[]->
    },
    location->,
}[0]`
Mar 3, 2021, 12:00 AM
Glad to hear it, Austin! šŸ™‚
Mar 3, 2021, 12:01 AM

Sanityā€“ build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?