withastro/astro · error · AstroError

DataCollectionEntryParseError

DataCollectionEntryParseError

Error message

**${entryId}** failed to parse: ${errorMessage}

What it means

The data collection layer parses `.json` files in your content directory with `JSON.parse`. When parsing throws, Astro wraps it as `DataCollectionEntryParseError` with the file path relative to the content dir and the underlying parse message (e.g. "Unexpected token"). Empty files are allowed; only invalid JSON triggers this.

Solutions

  1. Open the file named in the message and fix the syntax at the position given in the parse error.
  2. Remove comments, trailing commas, and quote all keys/values with double quotes.
  3. Run the file through a JSON validator (`npx jsonlint src/content/file.json`) or your editor's JSON language mode.
  4. If you need comments/relaxed syntax, store the data as `.yaml` instead, which the same layer supports.

Example fix

// before: src/content/authors.json
{
  "name": "Alice", // comment not allowed
}

// after
{
  "name": "Alice"
}
Defensive patterns

Strategy: validation

Validate before calling

// Prevalidate data collection JSON before building
import { readFileSync, readdirSync } from 'node:fs';
for (const f of readdirSync('src/content')) {
  if (!f.endsWith('.json')) continue;
  try {
    JSON.parse(readFileSync(`src/content/${f}`, 'utf-8'));
  } catch (e) {
    console.error(`${f}: ${(e as Error).message}`);
    process.exitCode = 1;
  }
}

Try / catch

try {
  await build();
} catch (err) {
  if (err instanceof Error && err.name === 'DataCollectionEntryParseError') {
    console.error(`Fix ${err.loc?.file ?? 'content file'}: ${err.message}`);
    return;
  }
  throw err;
}

Prevention

When it happens

Trigger: A non-empty `.json` entry in the content directory that is not valid JSON: trailing commas, comments, unquoted keys, single quotes, unescaped newlines in strings.

Common situations: Hand-editing data files and adding `// comments`; copy-pasting from docs or chat replies that include markdown fences; trailing comma after the last property.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


AI-assisted analysis of withastro/astro@52e6c34790 (2026-08-18). Data as JSON: /api/errors/5e3815daeca49691. Report an issue: GitHub.

Appendix: source

Thrown at packages/astro/src/core/config/settings.ts:58

		injectedRoutes: [],
		resolvedInjectedRoutes: [],
		pageExtensions: ['.astro', '.html', ...SUPPORTED_MARKDOWN_FILE_EXTENSIONS],
		contentEntryTypes: [markdownContentEntryType],
		dataEntryTypes: [
			{
				extensions: ['.json'],
				getEntryInfo({ contents, fileUrl }) {
					if (contents === undefined || contents === '') return { data: {} };

					const pathRelToContentDir = path.relative(
						fileURLToPath(contentDir),
						fileURLToPath(fileUrl),
					);
					let data;
					try {
						data = JSON.parse(contents);
					} catch (e) {
						throw new AstroError({
							...AstroErrorData.DataCollectionEntryParseError,
							message: AstroErrorData.DataCollectionEntryParseError.message(
								pathRelToContentDir,
								e instanceof Error ? e.message : 'contains invalid JSON.',
							),
							location: { file: fileUrl.pathname },
							stack: e instanceof Error ? e.stack : undefined,
						});
					}

					if (data == null || typeof data !== 'object') {
						throw new AstroError({
							...AstroErrorData.DataCollectionEntryParseError,
							message: AstroErrorData.DataCollectionEntryParseError.message(
								pathRelToContentDir,
								'data is not an object.',
							),
							location: { file: fileUrl.pathname },

View on GitHub (pinned to 52e6c34790)