Filter array of strings in Sanity query returning null
I can help you with filtering an array of strings in GROQ! Based on your example where platform is an array like ['ios', 'android'], here are the correct ways to filter it:
If you want to filter documents where platform contains a specific value:
*[_type == 'bannerQueue' && "ios" in platform] {
banners[]->{
_id,
bannerType,
"description": description[$lang],
"imageUrl": image.asset->url,
platform,
}
}[0]The in operator checks if a value exists in an array. So "ios" in platform returns true if the platform array contains "ios".
If you want to filter banners based on their platform array:
*[_type == 'bannerQueue'] {
banners[]->{
_id,
bannerType,
"description": description[$lang],
"imageUrl": image.asset->url,
platform,
}["ios" in platform || "android" in platform]
}[0]Here you're dereferencing the banners first with []->, then filtering the results with another filter that checks if the platform array contains your desired values.
Common reasons for getting null:
- Missing the
inoperator - You need"ios" in platform, notplatform == "ios" - Filter placement - Make sure your filter comes after the dereference operator (
->) if you're filtering the referenced documents - Array syntax - Remember to use
[]->when dereferencing an array of references
The key insight from the GROQ documentation is that the in operator is specifically designed for matching values in arrays: *["sci-fi" in genres] matches all documents where genres is an array containing the value "sci-fi".
Try the first example if you're filtering at the bannerQueue level, or the second if you need to filter the individual banners after dereferencing them!
Show original thread23 replies
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.