GROQ query: Get array of image URLs from array of objects

17 replies
Last updated: Apr 10, 2023
Hey everyone!Could anyone guide me on how I could do a groq query for an array of objects?

I want images to return an array of images with just their url…
groq`*[_type == "art"]{
      _createdAt,
      _id,
      "category": category.title,
      "slug": slug.current,
      "featureImage": featureImage.asset->url,
      images
    }`
AI Update

Looking at your query, you want to transform the images array to return just the URLs. In GROQ, you can use array projections to map over array elements and extract specific fields. Here's how to do it:

*[_type == "art"]{
  _createdAt,
  _id,
  "category": category.title,
  "slug": slug.current,
  "featureImage": featureImage.asset->url,
  "images": images[].asset->url
}

The key is images[].asset->url - this syntax does three things:

  1. images[] - Iterates over each element in the images array
  2. .asset-> - Follows the reference to the asset document
  3. .url - Extracts just the url field

This will give you an images field containing a flat array of URL strings like ["https://...", "https://..."] instead of an array of image objects.

If your images are stored differently (for example, if the URL is directly on the image object rather than on a referenced asset), you might need:

"images": images[].url

Or if they're image objects without references:

"images": images[].asset.url

The [] operator is really powerful in GROQ - it lets you project through arrays and extract exactly what you need. You can read more about projections in GROQ and check out the query cheat sheet for more array manipulation examples.

Show original thread
17 replies
You need to iterate over the array
[]
, dereference the asset
->
, then access the url:
'images': images[].asset->url
Thanks for that!
I should have noted I solved that with this just for reference in the future:
'images': images[] {
   alt,
   "url": image.asset->url
 }
Great!
In a similar issue, I am having trouble figuring out how to query a relationship
I have an array of a document type, artSeries. But I can’t figure out how to grab the information I need for the artSeries
{
    _createdAt: '2023-04-10T19:34:13Z',
    _id: 'homepageImages',
    _rev: 'QNM76uqvKuoc4uTWDKb5RR',
    _type: 'homepageImages',
    _updatedAt: '2023-04-10T19:35:32Z',
    artSeries: [ [Object] ],
    title: 'Homepage Images'
  }
So far I have tried doing something like the following:

groq`*[_type == "homepageImages" ]{
      _id,
      _createdAt,
      "artSeries": artSeries[] {
        not sure what I might do here haha
      },
    }`

What information are you trying to grab?
I need to get an array of artSeries documents that is referenced in the homepageImages
Right now the only info I get about each artSeries is this:
{"_key":"33077cdafee1","_ref":"4bd3b6a8-b353-4f14-9975-f3a847055509","_type":"reference"}
I just can’t figure out how to use the _ref to make another query for the artSeries with the same _id associated to it
Ah, you don’t need to use a named field in this case. Something like this should work:
*[_type == "homepageImages" ]{
      _id,
      _createdAt,
      artSeries[]->{
        //add projection here or remove the brackets if you want all of the fields.
      },
    }
Oh great! That was much easier than I was making it
And sorry just one other thing. Is there a way to order the artSeries by _createdAt?
Yeah try:
*[_type == "homepageImages" ]{
      _id,
      _createdAt,
      artSeries[]-> | order(_createdAt asc)
    }
Thank you!
You’re welcome!

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?