Issue with querying references in Sanity v3.47 resolved

28 replies
Last updated: Jun 20, 2024
Hello. I am having issues with querying references. I keep getting errors in parse response:
Unexpected non-white character after JSON.
I get response if query does not include references and weirdly the reference semi works when there is just one item not array of references. Anyone experiencing similar issues? I have just upgraded to v3.47.
AI Update

This isn't actually a v3.47-specific bug - you're running into Stega encoding, which is how Sanity's Visual Editing system works. The "Unexpected non-white character after JSON" error happens because Stega embeds invisible Unicode characters into string fields to enable click-to-edit overlays in preview/presentation mode.

These hidden characters break:

  • String comparisons ('left' === 'left' returns false)
  • JSON parsing when references contain encoded strings
  • Any business logic that checks string values

The reason it "semi works" with single items but fails with arrays is likely because the Stega characters are corrupting the JSON structure when multiple references are serialized together.

Solutions

Configure your Sanity client to exclude specific fields you use in conditionals or business logic:

import { createClient } from '@sanity/client'

const client = createClient({
  // ... other config
  stega: {
    filter: (props) => {
      const { sourcePath, value } = props;
      
      // Exclude specific fields by name
      if (sourcePath.includes('imageLocation') || 
          sourcePath.includes('slug') ||
          sourcePath[0] === 'yourReferenceField') {
        return false;
      }
      
      // Exclude short strings (likely enums/keys)
      if (typeof value === 'string' && value.length < 50) {
        return false;
      }
      
      return props.filterDefault(props);
    }
  }
})

2. Use stegaClean() for comparisons

Wrap fields in stegaClean() when doing logic:

import { stegaClean } from '@sanity/client/stega'

if (stegaClean(imageLocation) === 'left') {
  // Now this works correctly
}

// For arrays of references
const cleanedRefs = references.map(ref => ({
  ...ref,
  someField: stegaClean(ref.someField)
}))

3. Disable Stega globally

Turn off Stega and use manual data attributes instead:

// Client config
const client = createClient({
  // ... other config
  stega: false
})

Then annotate your JSX manually:

<div data-sanity={encodeDataAttribute(['imageLocation'])}>
  {imageLocation === 'left' && <LeftLayout />}
</div>

Why this happens

Stega encoding is Sanity's way of embedding metadata into strings to enable visual editing overlays without requiring you to manually add data attributes everywhere. The tradeoff is that it breaks string equality checks. There's an extensive GitHub discussion where Sanity's team explains the design decisions and acknowledges the pain points.

The issue affects all versions using Visual Editing/Presentation mode, not just v3.47. If you just upgraded and started seeing this, you likely enabled these features or they became enabled by default in your setup.

For your specific case with reference arrays: The filter approach (option 1) is your best bet. Add your reference field names to the filter to prevent Stega encoding on them, which should fix both the JSON parsing errors and the comparison issues.

Hey User! Can you send us over the query that is causing the issue?
*[_type == "platformHome"][0]{

_id,

title,

headline,

subheadline,

cards[]{

_key,

title,

subtitle,

platformLink->{"slug": slug.current}

}

}


I am trying to reference link to the page so I query said pages and extract just a slug. It was working before. I had the same issue with image url but it seems to be working now
If you remove all the fields like _id, title, etc and slowly add them back, which one causes the error?
I get response in studio. I use NextJS
At first glance that query looks right
I did it. Only platform link causes the problem
OH WAIT
I SEE
let me test something quick
When I do platformLink-&gt; only, o don’t get the error. But once I query fields in that document it breaks
Just tested other fields. Only slug causes the problem.
platformLink->{slug}

does this work?
platformLink->{slug} is working
Ah okay heres why:
Haha. Just came upWith the same
so not working?
It’s working
Thanks! I used the other solution before and it was working so not sure what changed.
platformLink-&gt;{slug}
^^ This works because
slug
is a property of the object already,
slug: slug.current
isnt possible because its a circular dependancy. In Javascript you cant assign something to itself using its own value
No problem! If you did something like
url: slug.current
that would work
Assuming no other field is named url
I found
“slug”: slug.current
in documentation hence used it before. Good to know different approaches.
Really? That surprising that works (and tbh I might be completely wrong but in my brain it makes sense) lol
Can you share where thats from?
I’ll look it up. Can’t remember from top of my head.
Thank you!
But if you test it in studio you get correct response hence I never questioned it.
Hmmm thats wild, good chance my reasoning is wrong if it works inside of vision

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?