The File Loader

One JSON or YAML File → Many Entries

The File Loader

When your entire dataset fits in a single file, `file()` loads it and turns the array (or object) into a collection.

3 min read Level 2/5 #astro#file-loader#json
What you'll learn
  • Use `file()` with JSON
  • Use `file()` with YAML
  • Pick a key for the entry id

When all your data lives in one file — authors, products, FAQ entries — use file().

A JSON File

[
  { "id": "ada",   "name": "Ada Lovelace", "twitter": "@ada"   },
  { "id": "grace", "name": "Grace Hopper",  "twitter": "@grace" }
]
import { defineCollection, z } from "astro:content";
import { file } from "astro/loaders";

const authors = defineCollection({
  loader: file("./src/data/authors.json"),
  schema: z.object({
    id: z.string(),
    name: z.string(),
    twitter: z.string().optional(),
  }),
});

Each item in the JSON array becomes an entry. The id field on each object is used as the entry’s id (or you can configure another field).

A YAML File

file() also accepts YAML:

- id: ada
  name: Ada Lovelace
  twitter: '@ada'
- id: grace
  name: Grace Hopper
  twitter: '@grace'

Same loader call, just point it at the .yaml file.

Object-Shaped Files

If the file is an object instead of an array, each KEY becomes an entry’s id:

{
  "ada":   { "name": "Ada Lovelace",  "twitter": "@ada"   },
  "grace": { "name": "Grace Hopper",   "twitter": "@grace" }
}

When to Use File vs Glob

SituationLoader
One file per entry, possibly with Markdown bodyglob
All entries in a single JSON/YAML filefile
Mix — long-form body + small metadata tableBoth! Two collections, one each

Up Next

Markdown as your content format.

Markdown →