GROQ: Flatten nested array and extract specific property (usedTime) from tasks
Based on your query structure, you're dealing with nested arrays and trying to flatten them while extracting a specific property. The key issue is that you need to properly flatten the nested tasks[].tasks[] structure and then project the usedTime property.
Here's how to achieve what you're after:
*[_type == "internalCustomers" && 'myMail@mail.com' in users[]->email]{
"totalHours": tasks[].tasks[].usedTime
}This syntax tasks[].tasks[].usedTime will:
- Iterate through the outer
tasksarray - For each item, iterate through the inner
tasksarray - Extract the
usedTimeproperty from each task - Automatically flatten the result into a one-dimensional array
The double array projection ([][]) handles the flattening for you - GROQ automatically flattens nested arrays when you chain array projections like this, as explained in the pipeline components documentation.
Filtering out null values
If you want GROQ to omit null/undefined values (which it sounds like you do), you can add a filter using the defined() function:
*[_type == "internalCustomers" && 'myMail@mail.com' in users[]->email]{
"totalHours": tasks[].tasks[defined(usedTime)].usedTime
}This filters the inner tasks array to only include items where usedTime is defined before extracting the values.
Why your attempts returned null
Looking at your attempts:
tasks[].tasks->usedTime- The->operator is for following references, not accessing propertiestasks[].tasks[]->usedTime- Same issue with->tasks[].tasks[]->.usedTime- Same reference operator issuetasks[].tasks.usedTime- Missing the second[]to properly iterate through the nested array
The correct syntax for accessing properties in nested arrays is to use [] for each array level, then access the property directly with dot notation. The -> operator is specifically for dereferencing document references, not for property access.
Show original thread10 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.