Can GROQ query JSON strings stored in document fields?
You're correct – GROQ cannot parse JSON strings stored as plain text within a field. If you have a field containing a string like "[ { \"id\":\"one\" }, { \"id\":\"two\"} ]", GROQ treats it as just a string value and has no built-in JSON parsing function to query the nested data.
GROQ is designed to work with structured JSON documents that are already parsed and stored in the Content Lake, not with JSON-as-string data. The query language expects your content to be properly structured when it's stored in Sanity.
Your Options
1. Store it as proper structured data (recommended)
Instead of storing JSON as a string, define a proper schema that matches your data structure:
{
name: 'myDocument',
type: 'document',
fields: [
{
name: 'data',
type: 'array',
of: [{
type: 'object',
fields: [
{name: 'id', type: 'string'}
]
}]
}
]
}Now you can query it with GROQ: *[_type == "myDocument" && "one" in data[].id]
2. Use a custom JSON field type
If you genuinely need to store arbitrary JSON, you could use a custom input component or plugin that accepts JSON input but stores it as parsed objects (not strings). The GROQ Arcade can help you test queries against JSON data structures.
3. Parse it in your application layer
If you must keep it as a string (maybe it's coming from an external source), fetch the document and parse the JSON string in your application code after retrieval, not in the GROQ query itself.
The key principle: GROQ works with Sanity's structured content model where data is stored as proper JSON objects and arrays, not as stringified JSON. Converting your string field to a properly structured field will give you the full power of GROQ queries.
Show original thread25 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.