👋 Next.js Conf 2024: Come build, party, run, and connect with us! See all events

Filtering out null responses in GROQ query for tags in blog, image, and imageCollections.

24 replies
Last updated: Mar 6, 2023
Hello! I am wondering how I can filter out all
null
responses in this GROQ query…My goal is to basically sift thru my entire response and get all the “tags” from blog, image, and imageCollections.

*[_type == "blogPost" && _id =="e79ed544-8747-4114-9461-3935791eb7bc"]{
  "blogTags": tags[]->{"slug": slug.current, tagName},
    
  "mainImageTags": mainImage->.tags[]->{"slug": slug.current, tagName},
    
  "imageCollectionTags": body[]{
        _type == 'imageCollectionRef' => @->{
          collectionImages[]->{tags[]}
        }
  }.collectionImages[].tags[]->{"slug": slug.current, tagName},
}
Mar 5, 2023, 6:47 PM
Here is the response:
Mar 5, 2023, 6:47 PM
IDK about how to do it using GROQ or sanity but if you are using JS or TS, you can check it by applying
if
conditional statements. But there'll be way in sanity and that would be better option then this one.
You can use it for temporary purpose.
Mar 5, 2023, 6:50 PM
Thanks! Yeah you mean
array.filter(item => item !== null)
?
Mar 5, 2023, 6:50 PM
Great! Thanks for sharing your answer with the community sir :saluting_face:
Mar 5, 2023, 6:51 PM
Yeah ideally I can do this with sanity groq query
Mar 5, 2023, 6:52 PM
Or maybe there is a better way to GROQ query for all nested “tags[]” fields?
Mar 5, 2023, 7:12 PM
Hey, Do you have any experience in making an array of images in a document?
Mar 5, 2023, 7:13 PM
user P
Hey here you can see how I created an array of strings, Is it possible with images too?
Mar 5, 2023, 7:25 PM
sorry wrong thread
Mar 5, 2023, 7:26 PM
You can filter the
tags
array inside of GROQ. Ex:
tags[@ != null]
or possibly:

tags[defined(@)]
Mar 6, 2023, 7:33 PM
thank you, so that would go inside of each
tags[]
?
Mar 6, 2023, 7:50 PM
Exactly. I also just remembered that if you’re dealing with an array of strings you can use the
array::compact()
function . I don’t believe it’ll work on an array of objects, though.
Mar 6, 2023, 7:56 PM
ok thanks
Mar 6, 2023, 8:02 PM
So I am still getting `null`…
*[_type == "blogPost" && _id =="e79ed544-8747-4114-9461-3935791eb7bc"]{
  "imageCollectionTags": body[]{
        _type == 'imageCollectionRef' => @ ->,
  }.collectionImages[]->.tags[@ != null]->{"slug": slug.current, tagName},
}
Mar 6, 2023, 8:03 PM
Im wondering if I should try making just an array of strings and trying compact
Mar 6, 2023, 8:04 PM
great that worked! now i will try to filter out the nulls the other way
Mar 6, 2023, 8:05 PM
will try to do it on object array
Mar 6, 2023, 8:05 PM
ooo it did!!
Mar 6, 2023, 8:06 PM
yesss this is gold thank you
Mar 6, 2023, 8:06 PM
mind blown
Mar 6, 2023, 8:06 PM
I love it when GROQ does something you didn’t think it could do.
Mar 6, 2023, 8:06 PM
is there a way to only get unique values and form a a set?
Mar 6, 2023, 8:07 PM
looks like array::unique
Mar 6, 2023, 8:08 PM
love it
Mar 6, 2023, 8:08 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?