GROQ Syntax Error: Conditional Field Selection for Multiple Document Types
Not silly at all! This is a common syntax issue with conditional projections in GROQ. The problem is that you're trying to use a conditional projection syntax that doesn't exist in GROQ.
The syntax { _type == "footer" => { ... } } isn't valid GROQ. What you're looking for is to use conditional projections inside the main projection object, not nested in another set of curly braces.
Here's the correct approach:
*[_type in ["banner", "footer"]] {
_type,
_type == "footer" => {
footerField1,
footerField2
},
_type == "banner" => {
bannerField1,
bannerField2
}
}The key difference from your original query is that the conditional (_type == "footer" =>) should be inside the main projection object as a direct property, not wrapped in extra curly braces.
According to the GROQ pipeline components documentation, the conditional projection syntax condition => {} expands the object on the right-hand side into the projection when the condition evaluates to true. This syntax is exactly equivalent to ...select(condition => {}).
So when you write:
{
_type == "footer" => {
footerField1,
footerField2
}
}GROQ evaluates the condition and if it's true, it expands those fields into the main projection object.
The error "expected '}' following object body" happened because GROQ saw your nested { ... } structure and got confused about where the projection object ended. The conditional operator => creates a conditional field directly, without needing the extra wrapper braces you had.
Show original thread12 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.