Querying image URLs in Sanity.io schema and data

22 replies
Last updated: May 26, 2023
Hi all,
I have an object as such:

defineField({
      name: "logo_set",
      title: "Logo Set",
      type: "array",
      of: [
        {
          name: "brand_logo",
          title: "Brand Logo",
          type: "image",
          fields: [
            {
              name: "alt",
              title: "Alternative text",
              type: "string",
            },
          ],
        },
      ],
    })
I’m trying to query the URLs of the images that will be uploaded, but I’m getting “null” as the value.

My current query:

logo_set[]{
    'logos': brand_logo.asset->url
}
What would be the correct way to fetch the URLs ?
AI Update

The issue is that you're trying to access a property brand_logo that doesn't exist in your array items. When you define an array with inline object types (not a named type), the array items are the objects themselves - they don't have an intermediate property name.

Your schema defines an array where each item is directly an image object. So you should access the asset reference directly without the brand_logo prefix:

logo_set[]{
  'logos': asset->url
}

Or even simpler, if you just want the URLs:

logo_set[].asset->url

The name: "brand_logo" in your schema is just a label for the Studio UI to identify the array item type, not a property in the actual data structure. Each item in the logo_set array is an image object with an asset reference and an alt field.

If you want to get both the URL and alt text, you could query like this:

logo_set[]{
  'url': asset->url,
  alt
}

Why your original query returned null: You were trying to access brand_logo.asset->url, but brand_logo doesn't exist as a property in your stored data. The array items are the image objects themselves, so you access asset directly on each array item.

What does this return?
logo_set[]{
  'logos': brand_logo.asset
}
its returns:
logo_set:[…] 2 items
  0:{…} 1 property
    logos:null
  1:{…} 1 property
    logos:null
Sounds like there is no asset. What does this return:
logo_set[]{
  'logos': brand_logo
}
same…
Heh, ok, so what do you get with just:
logo_set
Also, what does your entire query look like?
My entire query looks like this:
*[_type == "page"]{
        page_content[_type == "logo_row_section"]{
            logo_set[]
        }
    }
And the result of the above is:

[…] 1 item
 0:{…} 1 property
  page_content:[…] 1 item
   0:{…} 1 property
    logo_set:[…] 2 items
      0:{…} 4 properties
        _type:logo
        alt:Random 1
        _key:941bc656cfd4
        asset:{…} 2 properties
          _ref:image-e60765243a3f97511bffe0857bd962b4ad1a3196-380x250-png
          _type:reference
      1:{…} 4 properties
        _key:c16e65039613
        asset:{…} 2 properties
           _ref:image-36f944af4f008d94241194e04d320755ffcd6cd2-380x250-png
           _type:reference
        _type:logo
        alt:Random 2
There's nothing called
brand_logo
here
You could do
logo_set[] { asset->url }
Sorry my bad… I just changed the code, to try another apporach
Will try this:
logo_set[] { asset->url }
It returns:
Attribute or a string key expected
Sorry, just add
"url":
in front
logo_set[] { "url": asset->url }
Ah, yes! it works now !!!
Thanks a ton!

Can you give me some insight, as to why this query needs to be structured as such ?
You mean why the
"url"
part?
There is no property called
brand_logo
, so you cannot select it
Actually the array now looks like this:
defineField({
      name: "logo_set",
      title: "Logo Set",
      type: "array",
      of: [
        {
          name: "logo",
          title: "Logo",
          type: "image",
          fields: [
            {
              name: "alt",
              title: "Alternative text",
              type: "string",
            },
          ],
        },
      ],
    }),
Hence there’s no
brand_logo

But this query:
logo_set[] { "url": asset->url }
worked
What I wanted to know is, why doesn’t this work:
logo_set[] { "url": logo.asset->url }
Well, it looks like your data doesn't match your schema
If your data followed your schema, it would be
logo.asset
, but there's no
logo
in your current array
the “image” type has the name and title of “logo”
Not in your current data
of
is an array of objects, and
logo
isn't a field name on each object. I think you need to read more about how schemas work.

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?