Files

JSON

How to define, write and query JSON data.

Define Collection

content.config.ts
import { defineCollection, defineContentConfig, z } from '@nuxt/content'

export default defineContentConfig({
  collections: {
    authors: defineCollection({
      type: 'data',
      source: 'authors/**.json',
      schema: z.object({
        name: z.string(),
        avatar: z.string(),
        url: z.string()
      })
    })
  }
})

Create .json files

Create authors files in content/authors/ directory.

{
  "name": "Ahad Birang",
  "avatar": "https://avatars.githubusercontent.com/u/2047945?v=4",
  "url": "https://github.com/farnabaz"
}
Each file in data collection should contain only one object, therefore having top level array in a JSON file will cause invalid result in query time.

Query Data

Now we can query authors:

// Find a single author
const theAuthor = await queryCollection('authors')
  .where('stem', '=', 'larbish')
  .first()

// Get all authors
const authors = await queryCollection('authors')
  .order('name', 'DESC')
  .all()