How to Wrap Nested Arrays in Objects
The limitation about arrays not containing arrays is a data store constraint in Sanity, and the workaround is actually pretty straightforward! Instead of nesting an array directly inside another array, you wrap the inner array in an object type.
Here's a practical example to illustrate this:
β This won't work (array directly inside array):
{
name: 'categories',
type: 'array',
of: [
{
type: 'array', // This is not allowed!
of: [{type: 'string'}]
}
]
}β This will work (wrap the inner array in an object):
{
name: 'categories',
type: 'array',
of: [
{
type: 'object',
name: 'categoryGroup',
fields: [
{
name: 'items',
type: 'array', // Now the array is inside an object
of: [{type: 'string'}]
}
]
}
]
}Here's a more real-world example - let's say you want to create a FAQ section where each category has multiple questions:
{
name: 'faqSection',
type: 'array',
title: 'FAQ Categories',
of: [
{
type: 'object',
name: 'faqCategory',
fields: [
{
name: 'categoryName',
type: 'string',
title: 'Category Name'
},
{
name: 'questions',
type: 'array', // This array is wrapped in the object above
title: 'Questions',
of: [
{
type: 'object',
fields: [
{name: 'question', type: 'string'},
{name: 'answer', type: 'text'}
]
}
]
}
]
}
]
}The key insight is that the data store limitation prevents array β array directly, but it's perfectly fine to do array β object β array. The object acts as a container that gives the inner array its own scope. This also has the benefit of letting you add additional fields alongside your nested array, which often makes your content model more flexible anyway!
When you query this data with GROQ, you'll just need to account for the object wrapper in your query path, but otherwise it works exactly as you'd expect. You can learn more about array fields and object types in the Sanity documentation.
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.