✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

How to find documents containing bad data in a nested custom component using Groq query.

5 replies
Last updated: Apr 3, 2022
We have managed to import some bad data in some documents, in a nested custom component, and I am trying to write a groq query to find what documents contain bad data.The problem is that some articles have ended up with a mark on a custom block, which ends up giving an error when we try to render it, because the marks aren’t a part of the schema. an example of such a bad block:

  "_key": "afd98068bfc1",
  "_type": "bilde",
  "altText": "",
  "caption": "",
  "image": {
    "_type": "image",
    "asset": {
      "_ref": "image-b3d77a3ad3970d959d3bf83d6d2c02ad320cbb71-1000x681-jpg",
      "_type": "reference"
  "marks": [
      "_key": "3c540f80b489",
      "_type": "link",
      "href": "<https://some-url>"
this lives in a nested type, so the structure is something like this

brodtekst[_type == 'hjelpeartikkel.expandCollapse'].innhold[_type == 'bilde']
and I am trying to find all documents containing
with the bad
array, but I can’t seem to find the right filtering to useI have tried something like this:

*[_type == 'hjelpeartikkel'
  && brodtekst[_type == 'hjelpeartikkel.expandCollapse'].innhold[_type == 'bilde'].marks[] != null]{
    brodtekst[_type == 'hjelpeartikkel.expandCollapse'] {
      innhold[_type == 'bilde'] {
but that ends up returning documents where there are no
, I’ve also tried to
count(brodtekst[_type == 'hjelpeartikkel.expandCollapse'].innhold[_type == 'bilde'].marks[]) > 0
and similar things, but they all end up either matching 0 documents, or too many documents. any pointers on how I can achieve this filter, or is it easier to just fetch all and manually go through them in javascript?
Apr 3, 2022, 7:26 AM
Hi User. I’m curious if this gets what you’re after:

*[_type == 'hjelpeartikkel'
  && brodtekst[_type == 'hjelpeartikkel.expandCollapse'].innhold[_type == 'bilde' && marks[] != null]]
Apr 3, 2022, 4:42 PM
hmm, no, can’t seem to get that to match any documents (and I know there is still at least one such document)
Apr 3, 2022, 4:51 PM
but I think
*[_type == 'hjelpeartikkel' && count(brodtekst[_type == 'hjelpeartikkel.expandCollapse'].innhold[_type == 'bilde' && marks[] != null]) > 0]{
might have done it
Apr 3, 2022, 4:53 PM
yeah it did, thanks for the help
user A
Apr 3, 2022, 4:55 PM
Great! Glad you got it worked out! 🙌
Apr 3, 2022, 4:58 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?