Table of Contents

The Error

Error: Error parsing JSON: invalid character

What Causes This

JSON in your configuration has invalid syntax: trailing commas, single quotes, unescaped characters, or BOM characters.

How to Fix It

Solution 1: Use jsonencode()

resource "aws_iam_policy" "main" {
  policy = jsonencode({
    Version = "2012-10-17"
    Statement = [{
      Effect   = "Allow"
      Action   = "s3:GetObject"
      Resource = "*"
    }]
  })
}

Solution 2: Validate JSON

python3 -m json.tool policy.json
jq . policy.json

Solution 3: Remove BOM Characters

sed -i '1s/^\xEF\xBB\xBF//' policy.json

Prevention Tips

  1. Pin provider versions — avoid surprise breaking changes
  2. Use CI/CD — catch errors before they hit production
  3. Test with terraform plan — always review before applying
  4. Keep Terraform updated — newer versions have better error messages
  5. Use terraform validate — catches syntax errors early

Hands-On Courses

Conclusion

This error is common and fixable. Follow the solutions above, and check our Terraform course for hands-on training that covers real-world troubleshooting scenarios.