Fix: Data Collection Entry Failed to Parse in Astro

Error message:
Data collection entry failed to parse.
Content Collections 2025-01-25

What Causes This Error?

This error occurs when a data collection file (JSON, YAML, or TOML) has invalid syntax and cannot be parsed. Common causes include missing commas, mismatched brackets, or incorrect indentation.

The Problem

// src/content/settings/config.json
{
  "name": "My Site"
  "url": "https://example.com"  // ❌ Missing comma
}
# src/content/settings/nav.yaml
items:
- label: Home
  url: /
 - label: About  # ❌ Wrong indentation
   url: /about

The Fix

Fix JSON Syntax

// ✅ Valid JSON
{
  "name": "My Site",
  "url": "https://example.com",
  "enabled": true
}

Fix YAML Syntax

# ✅ Valid YAML
items:
  - label: Home
    url: /
  - label: About
    url: /about

Common Scenarios

JSON Errors

// ❌ Trailing comma
{
  "name": "Site",
  "items": ["a", "b", "c",]
}

// ✅ No trailing comma
{
  "name": "Site",
  "items": ["a", "b", "c"]
}
// ❌ Single quotes
{
  'name': 'Site'
}

// ✅ Double quotes
{
  "name": "Site"
}
// ❌ Unquoted keys
{
  name: "Site"
}

// ✅ Quoted keys
{
  "name": "Site"
}

YAML Errors

# ❌ Inconsistent indentation
items:
  - name: First
   value: 1      # Wrong indent
  - name: Second
    value: 2

# ✅ Consistent indentation (2 spaces)
items:
  - name: First
    value: 1
  - name: Second
    value: 2
# ❌ Unquoted special characters
message: Hello: World!

# ✅ Quote strings with colons
message: "Hello: World!"
# ❌ Tab characters
items:
	- item1    # Tab character

# ✅ Spaces only
items:
  - item1    # 2 spaces

TOML Errors

# ❌ Missing quotes
name = My Site

# ✅ Quoted strings
name = "My Site"
# ❌ Invalid array
tags = [tag1, tag2]

# ✅ Quoted array items
tags = ["tag1", "tag2"]

Validate Before Committing

# Validate JSON
cat src/content/settings/config.json | jq .

# Validate YAML (with yq)
yq e '.' src/content/settings/nav.yaml

# Online validators
# https://jsonlint.com/
# https://www.yamllint.com/

Using VS Code Extensions

Recommended extensions:
- JSON: Built-in validation
- YAML: "YAML" by Red Hat
- TOML: "Even Better TOML"

These highlight syntax errors in real-time

Common Parse Error Messages

Error: Failed to parse settings/config.json
Unexpected token } in JSON at position 42

→ Look for missing comma before position 42
Error: Failed to parse settings/nav.yaml
bad indentation of a mapping entry at line 5

→ Check indentation at line 5

Escape Special Characters

// JSON escaping
{
  "quote": "He said \"Hello\"",
  "path": "C:\\Users\\name",
  "newline": "Line 1\nLine 2"
}
# YAML escaping
quote: 'He said "Hello"'
path: "C:\\Users\\name"
multiline: |
  Line 1
  Line 2

Schema Validation vs Parse Errors

Parse error: File syntax is invalid
→ Fix JSON/YAML/TOML syntax

Schema error: Data doesn't match schema
→ Fix data values (types, missing fields)

Parse errors must be fixed first!

Quick Checklist

  • JSON: Use double quotes, no trailing commas
  • YAML: Use consistent spaces (not tabs)
  • YAML: Quote strings with special characters
  • Use linters/validators to catch errors
  • Check error message for line number
  • Test with online validators