This error occurs when Terraform attempts to parse JSON configuration but encounters an unexpected character. It's commonly caused by syntax errors in JSON files, invalid file encoding, or corrupted data containing special characters.
When Terraform reads a JSON file (including .tfvars.json, S3 bucket policies, Azure templates, or other JSON resources), its JSON parser encounters a character it cannot interpret. This could be a visible character (like a stray quote or comma) or an invisible character (like a UTF-8 BOM or non-ASCII byte). The error typically appears when: - Terraform is parsing terraform.tfvars.json or *.auto.tfvars.json files - Processing JSON templates for cloud resources (AWS policies, Azure ARM templates) - Reading JSON encoded in resource arguments or outputs The "invalid character" message may specify which character caused the issue, such as '$', 'ï', '"', or other values, helping identify the exact problem location.
Use a JSON validator to identify syntax errors. Check for missing commas, trailing commas, and unescaped characters.
# Use jq to validate a JSON file
jq . your-file.json
# Or use a built-in check
python3 -m json.tool your-file.jsonIf the validator shows an error, it will pinpoint the line and column. Look for:
- Missing commas between object properties
- Trailing commas (JSON doesn't allow them)
- Unescaped backslashes or quotes
- Single quotes instead of double quotes
The UTF-8 BOM (Byte Order Mark) or non-UTF-8 encoding is a common cause. On Linux/macOS:
# Check for BOM marker (shows 'EFBBBF' for UTF-8 BOM)
xxd -l 16 your-file.json
# Check file encoding
file your-file.json
# Remove BOM if present
sed -i '1s/^\xEF\xBB\xBF//' your-file.json
# Or use vim
vim your-file.json
# Then in vim: :set fileencoding=utf-8 :wqOn Windows, use a text editor like VS Code and:
1. Open the file
2. Click the encoding indicator (bottom right)
3. Select "UTF-8" (without BOM)
Look for hidden characters that the error message references. Use a hex dump to see raw bytes:
# Show hex dump with ASCII
hexdump -C your-file.json | head -20
# Search for the problematic character mentioned in the error
# If error says 'ï' (UTF-8 BOM first byte), look for EF BFIf you see unusual bytes (like C3 AF for the character 'ï'), the file has encoding issues. Re-save in UTF-8 without BOM.
If using JSON in template_body or similar fields, ensure variables and special characters are properly escaped:
# WRONG: Unescaped $ breaks JSON
template_body = jsonencode({
properties = {
value = "$variable"
}
})
# CORRECT: Use Terraform variable interpolation
template_body = jsonencode({
properties = {
value = var.my_variable
}
})
# Or escape if the $ is literal
template_body = jsonencode({
properties = {
value = "\$literal_dollar_sign"
}
})Use Terraform's jsonencode() function to safely convert HCL values to JSON.
If converting from HCL to JSON, ensure proper syntax:
# .tfvars (HCL format)
image_id = "ami-abc123"
availability_zones = ["us-west-1a", "us-west-1c"]# .tfvars.json (JSON format) - must be valid JSON
{
"image_id": "ami-abc123",
"availability_zones": ["us-west-1a", "us-west-1c"]
}Common mistakes:
- Single quotes instead of double quotes
- HCL comments (//, #) in JSON files
- Unquoted keys in JSON
- Trailing commas in JSON objects or arrays
Verify with: terraform validate
After fixing the file, validate your entire Terraform configuration:
terraform validateThis checks syntax across all .tf and .tfvars files. If it passes, the JSON parsing error is resolved.
UTF-8 BOM Detection: The UTF-8 BOM is three bytes (EF BB BF) at the file start. Many text editors auto-detect it, but some tools (especially older ones) don't handle it gracefully. Unix/Linux tools often fail silently with BOM-marked files.
JSON vs HCL: Terraform supports both .tfvars (HCL) and .tfvars.json (JSON) formats. Mixing them or using wrong syntax is a common source of errors. If unsure, use HCL format as it's more forgiving.
Azure Template Bodies: When passing JSON to Azure resources via template_body, the entire JSON must be a valid string. Use jsonencode() to ensure proper escaping rather than hand-crafting JSON strings.
Variable Interpolation: Variables in JSON must be interpolated at the HCL level (before JSON encoding), not within JSON string literals. Use jsonencode() to wrap Terraform values safely.
Error: Error rendering template: template not found
How to fix "template not found" error in Terraform
Error: Error generating private key
How to fix 'Error generating private key' in Terraform
Error creating Kubernetes Service: field is immutable
How to fix "field is immutable" errors in Terraform
Error: Error creating local file: open: permission denied
How to fix "Error creating local file: permission denied" in Terraform
Error: line endings have changed from CRLF to LF
Line endings have changed from CRLF to LF in Terraform