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

Syntax for Outputting an Array in GROQ

5 replies
Last updated: Feb 2, 2023
When I use a variable that is an array in groq it seems to output a comma seperated string, instead of an array of strings. Whats the correct syntax for this to be an array?
*[_type == 'portfolio' && _id in ['${ids}'] ]{
This is what its currently printing

*[_type == 'portfolio' && _id in ['416927ba-3c52-40f9-b3eb-c41d18010b29,c34dedda-4325-49c6-8270-08084c867213,aff6dd72-add1-44e0-bc14-784a8f5fba98,bb15831f-c31f-4020-bf20-157d97ab278f']]{
Feb 1, 2023, 6:34 AM
join the ids with
","
as separators
*[_type == 'portfolio' && _id in ['${ids.join("','")}'] ]{
Feb 1, 2023, 7:33 AM
You’ll want to remove the single quotes around your ids variable. You’re currently explicitly making a string.
Feb 1, 2023, 4:23 PM
Thanks
user B
that worked perfectly
Feb 2, 2023, 12:32 AM
Not entirely sure why a join worked if it was already outputting comma separated string... but that seems to be working.
Feb 2, 2023, 12:33 AM
In your example
['${ids}']
becomes ``['a,b,c,d']``, which is a single id. In my example you counter the opening and closing
'
by adding multiple
'
between the items, creating
['a','b','c','d']
.
Feb 2, 2023, 6:08 AM

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?