How to join array of strings into single comma-separated string in GROQ?
Great question! Yes, GROQ has a built-in function for exactly this: array::join().
You can modify your query like this:
*[_type == "technologyProduct"]{
"sectors": array::join(targetIndustries[]->title, ", "),
}The array::join() function takes two parameters:
- An array of strings, numbers, or booleans
- A separator string (in your case
", "for comma-separated)
It returns a single concatenated string.
According to the GROQ Functions Reference, the syntax is:
array::join(source <array[string|number|boolean]>, separator<string>) <string>So in your case, targetIndustries[]->title produces an array of strings, and array::join() combines them into a single comma-separated string.
Note that if any element in the array isn't a string, number, or boolean, the function will return null. Also, if the input itself isn't an array (like if it's a single value), it will return null as well.
You can find more examples in the Query Cheat Sheet which shows examples like array::join(["a", "b", "c"], ".") returning "a.b.c".
Show original thread7 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.