👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Filtering out null/empty entries in GROQ subqueries for array fields in Sanity.io

5 replies
Last updated: Mar 19, 2022
Hi, general GROQ filter/subquery question… Is it possible to subquery an array field to filter out null/empty entries? For example, if you had a page builder schema with an array of different objects (text, image, video etc…) with a schema looking like this:

export default {
	name: 'page',
	title: 'Page',
	type: 'document',
	fields: [
		{
			name: 'content',
			type:'array', 
			of: 
			[
				{ type: 'page-block-hero' },
				{ type: 'page-block-text' },
				{ type: 'page-block-image' },
				{ type: 'page-block-video' },
				{ type: 'page-block-cta' },
				{ type: 'page-block-carousel' }
			]
		}
	]
}
I have a GROQ query that currently looks like this:


*[_type == "page" && slug.current == $slug]
{
		'content': content[]
		{
			'hero': hero->
			{
				heading,
				subheading
			},

			'image': image->
			{
				'src': file.asset->url,
				caption
			}
			...
		}
}
Trying to figure out how to filter each content type as to null out ones that don’t have any content…


*[_type == "page" && slug.current == $slug]
{
		'content': content[]
		{
			'hero': *[hero.heading != null]
			{
				heading,
				subheading
			},

			'image': *[image.file != null]->
			{
				'src': file.asset->url,
				caption
			}
			...
		}
}
Wondering A) is this possible with GROQ and B) What would a query look like to accomplish this…

Thanks!
Mar 19, 2022, 6:13 PM
There is some documentation on conditionals at this spot in the reference: https://www.sanity.io/docs/query-cheat-sheet#64a36d80be73
There are also two functions that might be relevant as far as what to base the conditions on -- and this is based on my understanding and a little experience -- a
defined( ) function that catches when something exists and coalesce( ) where you can cast a wide net out to things that may be non-null and the first one in your list that's valid will be what gets returned.
Mar 19, 2022, 7:09 PM
Thanks
user S
! Will take a look
Mar 19, 2022, 7:10 PM
Completely missed the conditionals block of the cheatsheet… looks very promising
Mar 19, 2022, 7:12 PM
user E
It's the only way I could make some of the things I wanted possible. And yeah, there are quite a many pages in the docs, but the full power of Sanity is pretty exhaustive. So I have gone through all the pages but people still bring stuff up and I am like how did I miss that?
I am just another user, but I have thought about making a guide for things like Desk Structure because it is a little abstract without examples (especially with such similar naming conventions), just taking one kind of task and applying the same kinds of things to it so people can see how it works in context. Lemme know how it turns out for you!
Mar 19, 2022, 9:18 PM
Yes agreed! This is my first project with Sanity and I have been continually impressed with how powerful GROQ is compared to GraphQL. I think that’s a great idea having a sanity Cookbook of sorts for common/useful tasks and customizations — I’ve been thinking the same thing as I figure various pieces out…
Mar 19, 2022, 9:21 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?