🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Querying specific fields in an array in Sanity schema

3 replies
Last updated: Jun 10, 2023
Is there a way to query all fields of a certain type (e.g all those of type string) among the fields of an array?
If I have this schema:

export default {
  name: "exampleSchema",
  title: "Example Schema",
  type: "document",
  fields: [
    {
      name: "objects",
      title: "Objects",
      type: "array",
      of: [
        {
          name: "object",
          type: "object",
          fields: [
            {
              name: "title",
              title: "Title",
              type: "string",
            },
            {
              name: "url",
              title: "URL",
              type: "slug",
            },
            {
              name: "randomString",
              title: "Random String",
              type: "string",
            },
          ],
        },
        {
          name: "objectTwo",
          type: "object",
          fields: [
            {
              name: "name",
              title: "Name",
              type: "string",
            },
            {
              name: "url",
              title: "URL",
              type: "slug",
            },
            {
              name: "evenMoreRandomString",
              title: "Even More Random String",
              type: "string",
            },
          ],
        },
      ],
    },
  ],
};
I would like to retrieve something like this:

[…] 1 item
    0:{…} 1 property
    objects:[…] 2 items
        0:{…} 2 properties
            title: asd
            randomString: asdasdasd
        1:{…} 2 properties
            name: asd
            evenMoreRandomString: asdasdasd
I thought maybe I could do something like this:

*[_type == "exampleSchema"] {
  objects[] {
    "stringFields": getFields(@)[_type == "string"]
  }
}
Thanks a lot!
Jun 10, 2023, 3:16 PM
Hi
user W
. Unfortunately, those values won’t be accessible using GROQ since they’re not stored with the data. Each schema type exists for the Studio, but the Content Lake won’t care about them. You could edit the JSON and make
objectTwo
a string,
object.title
an array, etc.
It might be possible to give guidance if we know a bit more about why you want to target and return the strings. One possibility might be to infer all the strings from the schema and then use GROQ to query for those values.
Jun 10, 2023, 3:53 PM
Thanks a lot, (Removed Name). I found a workaround.My schema was actually:

footerMenu(document)
   columns(array)
      terms(document)
      help(document)

Jun 10, 2023, 6:52 PM
Thanks a lot, (Removed Name). I found a workaround.My schema was actually:

footerMenu(document)
   columns(array) of:
      terms(document)
      help(document)

terms(document)
   title
   slug
   legalNotice(object)
      title
      slug
   privacyPolicy(object)
      title
      slug
   ...

help(document)
   title
   slug
   shipping(object)
      title
      slug
   exchanges(object)
      title
      slug
   ...
What I wanted was to query all objects within each column of the columns array. So that I would get:


legalNotice {
   title
   slug
},
privacyPolicy {
   title
   slug
},
shipping {
   title
   slug
},
exchanges {
   title
   slug
}
My solution was simply to change the schemas and put the objects inside an array:


terms(document)
   title
   slug
   subItems(array)
      legalNotice(object)
         title
         slug
      privacyPolicy(object)
         title
         slug
   ...

help(document)
   title
   slug
   subItems(array)
      shipping(object)
         title
         slug
      exchanges(object)
         title
         slug
   ...
I guess that was the obvious solution, right? I'm new to groq.

The only problem is I had to manually reenter all data inside subItems.

Thanks a lot!
Jun 10, 2023, 7:00 PM

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?