🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Content & MDX••By Komal Nakrani

Astro JSON Data: Use JSON and YAML Files

Use Astro JSON data and YAML files: import small JSON files directly, or load JSON, YAML and TOML into typed content collections with the file() loader.

#Astro#Astro JSON Data#YAML#Content Collections

Astro gives you two ways to use JSON data. For a small config-style file, import it directly: import nav from '../data/nav.json'. For lists you query, validate or link to (team members, products, FAQs), load the file into a content collection with the file() loader, which reads JSON, YAML and TOML and checks every entry against a schema.

Key Takeaways

  • Direct JSON imports work out of the box and are fully typed by TypeScript.
  • file() parses .json, .yml, .yaml and .toml based on the extension.
  • Each item needs a unique id (or slug), or the file can be an object keyed by ID.
  • Collections give you schema validation, getEntry() and references between collections.

Import JSON directly

// src/data/site.json
{
  "name": "Example Studio",
  "email": "[email protected]",
  "social": [
    { "label": "GitHub", "href": "https://github.com/example" },
    { "label": "Mastodon", "href": "https://mastodon.social/@example" }
  ]
}
---
// src/components/Footer.astro
import site from '../data/site.json';
---
<footer>
  <p>{site.name} · <a href={`mailto:${site.email}`}>{site.email}</a></p>
  <ul>{site.social.map((s) => <li><a href={s.href}>{s.label}</a></li>)}</ul>
</footer>

Vite handles the import and TypeScript infers the types from the JSON, so site.social[0].href autocompletes. The data is bundled at build time. This is ideal for site settings, navigation and small lookup tables. YAML files can’t be imported this way without a Vite plugin, which is one reason to use collections for YAML.

Load data into a collection

# src/data/team.yaml
- id: priya
  name: Priya Shah
  role: Lead designer
  photo: ./photos/priya.jpg
- id: tom
  name: Tom Becker
  role: Developer
  photo: ./photos/tom.jpg
// src/content.config.ts
import { defineCollection } from 'astro:content';
import { file } from 'astro/loaders';
import { z } from 'astro/zod';

const team = defineCollection({
  loader: file('src/data/team.yaml'),
  schema: ({ image }) =>
    z.object({
      name: z.string(),
      role: z.string(),
      photo: image(),
    }),
});

export const collections = { team };
---
// src/pages/about.astro (excerpt)
import { getCollection } from 'astro:content';
import { Image } from 'astro:assets';
const team = await getCollection('team');
---
{team.map(({ data }) => (
  <figure>
    <Image src={data.photo} alt={data.name} width={320} height={320} />
    <figcaption>{data.name}, {data.role}</figcaption>
  </figure>
))}

The loader picks the parser from the extension: JSON.parse for .json, a YAML parser for .yml and .yaml, and a TOML parser for .toml. Paths in file() are relative to the project root. Image paths inside the data resolve relative to the data file, as with frontmatter (see the frontmatter image tip).

Array or object

The file can be an array of objects with id (or slug) fields, as above, or an object whose keys become IDs:

// src/data/authors.json
{
  "komal": { "name": "Komal", "bio": "Writes about Astro." },
  "alex": { "name": "Alex", "bio": "Builds themes." }
}

Missing or duplicate IDs are reported during the build. Nested data, such as a JSON file with { "items": [...] }, needs a custom parser that returns the array: file('src/data/feed.json', { parser: (text) => JSON.parse(text).items }). The CSV tip uses the same option for other formats.

Why bother with a collection

  • Validation: a typo like rol: fails the build instead of rendering a blank.
  • Lookups: getEntry('authors', 'komal') finds one item by ID.
  • References: a blog post’s author: komal can be a reference('authors'), checked at build time. See the collection references tip.
  • Dev reloads: edits to the data file update the dev server without a restart.

The Content Collections docs page and content loaders guide go deeper. Theme sites often keep team, services or pricing in a data file like this; a professional services theme is a typical example of where it pays off.

FAQ

How do I import a JSON file in Astro?

Use a normal import in any component or page: import data from '../data/file.json'. TypeScript infers the types and the data is included at build time.

Can Astro load YAML files?

Yes, through the file() loader: defineCollection({ loader: file('src/data/team.yaml') }). It parses .yml and .yaml automatically. Direct YAML imports need a Vite plugin.

What does the file() loader need in each item?

An id or slug field that’s unique across the file, unless the file is an object whose keys act as IDs. Entries without an ID are skipped with an error in the build log.