GROQ Query Response - Making a Separate Property the Property an Object
Yes, absolutely! You can achieve this using the GROQ spread operator (...) to merge the lqip property into the mainImage object. Here's how:
*[_type == "post"][0] {
"mainImage": {
...mainImage,
"lqip": mainImage.asset->metadata.lqip
}
}This works by:
- Creating a new
mainImageobject in the projection - Using
...mainImageto spread all the existing properties from the originalmainImagefield (including_type,altText, andasset) - Adding the
lqipproperty by dereferencing the asset metadata
The result will be exactly what you're looking for:
{
"mainImage": {
"_type": "image",
"altText": "Mountains with pink sky and turquoise water",
"asset": {
"_ref": "image-db89f238f31f609d2cac163248de2105d4c1f06c-3374x2000-jpg",
"_type": "reference"
},
"lqip": "data:image/jpeg;base64,/9j/2wBDAAYEBQYFBAYGBQYHBw..."
}
}The ellipsis/spread operator (...) in GROQ is perfect for this kind of object composition where you want to keep all existing fields while adding new ones. Just note that the spread operator is always evaluated first regardless of where it appears in the projection, so any properties you define after it will override properties with the same name from the spread.
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.