gofr-dev/gofr · error
failed to parse JSON config file %s: %w
Error message
failed to parse JSON config file %s: %w
What it means
LoadPermissions failed to parse the RBAC config file as JSON. json.Unmarshal returned an error (invalid syntax, wrong types, trailing data), so the RBAC configuration cannot be loaded and startup fails. The file path is embedded for locating the file.
Source
Thrown at pkg/gofr/rbac/config.go:151
// Dependencies (logger, metrics, tracer) are optional and can be set after loading.
func LoadPermissions(path string, logger datasource.Logger, metrics container.Metrics, tracer trace.Tracer) (*Config, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read RBAC config file %s: %w", path, err)
}
var config Config
// Detect file format by extension
ext := strings.ToLower(filepath.Ext(path))
switch ext {
case ".yaml", ".yml":
if err := yaml.Unmarshal(data, &config); err != nil {
return nil, fmt.Errorf("failed to parse YAML config file %s: %w", path, err)
}
case ".json", "":
if err := json.Unmarshal(data, &config); err != nil {
return nil, fmt.Errorf("failed to parse JSON config file %s: %w", path, err)
}
default:
return nil, fmt.Errorf("unsupported config file format: %s (supported: .json, .yaml, .yml): %w", ext, errUnsupportedFormat)
}
// Set dependencies
config.Logger = logger
config.Metrics = metrics
config.Tracer = tracer
// Initialize mux router for pattern matching
// Use StrictSlash(false) to match the application router's behavior
config.muxRouter = mux.NewRouter().StrictSlash(false)
// Validate config before processing
if err := config.validate(); err != nil {
return nil, fmt.Errorf("invalid RBAC config: %w", err)
}View on GitHub (pinned to 187eb24962)
Solutions
- Run the file through a JSON validator (jq or jsonlint) and fix the syntax error at the reported offset.
- Remove JSON-illegal constructs: comments, trailing commas, single quotes.
- Check field types against the Config struct (e.g. requiredPermissions must be an array).
- If the content is YAML, rename to .yaml/.yml so the YAML branch parses it.
Example fix
// before (rbac.json)
{ "endpoints": [ { "path": "/api/users", "requiredPermissions": ["users:read"], } ] }
// after
{ "endpoints": [ { "path": "/api/users", "requiredPermissions": ["users:read"] } ] } Defensive patterns
Strategy: validation
Validate before calling
data, err := os.ReadFile(path)
if err != nil { return err }
ext := strings.ToLower(filepath.Ext(path))
if ext == ".json" || ext == "" {
var probe map[string]any
if err := json.Unmarshal(data, &probe); err != nil {
return fmt.Errorf("rbac json %s invalid: %w", path, err)
}
} Try / catch
config, err := rbac.LoadPermissions(path, logger, metrics, tracer)
if err != nil && strings.Contains(err.Error(), "failed to parse JSON") {
logger.Fatalf("invalid JSON in %s: %v", path, err)
} Prevention
- Run jq over generated JSON configs in CI.
- Forbid comments and trailing commas (no JSON5 in .json files).
- Generate config from a typed struct via json.Marshal instead of hand-editing.
- Keep extension-less config files strictly JSON.
When it happens
Trigger: Calling EnableRBAC/LoadPermissions with a .json file (or no extension) containing invalid JSON: trailing commas, single quotes, unquoted keys, comments, BOM, or values of the wrong type for the Config struct.
Common situations: Config generated by hand or copied from docs with comments; an environment-specific pipeline emitted malformed JSON; a file with no extension is actually YAML; type mismatch like permissions as a string instead of an array.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- failed to parse YAML config file %s: %w
- unsupported config file format: %s (supported: .json, .yaml,
- invalid RBAC config: %w
- failed to process unified config: %w
- endpoint[%d]: %w: %s
AI-assisted analysis of gofr-dev/gofr@187eb24962 (2026-09-01).
Data as JSON: /api/errors/4df424aab0069a5a.
Report an issue: GitHub.