hashicorp/nomad · error
json format does not support template option.
Error message
json format does not support template option.
What it means
DataFormat in command/data_format.go returns this error when format is "json" but a non-empty template string is also supplied. The JSON formatter renders data as JSON and cannot accept a Go text/template, so the conflicting combination is rejected outright.
Source
Thrown at command/data_format.go:33
var (
jsonHandlePretty = &codec.JsonHandle{
HTMLCharsAsIs: true,
Indent: 4,
}
)
// DataFormatter is a transformer of the data.
type DataFormatter interface {
// TransformData should return transformed string data.
TransformData(any) (string, error)
}
// DataFormat returns the data formatter specified format.
func DataFormat(format, tmpl string) (DataFormatter, error) {
switch format {
case "json":
if len(tmpl) > 0 {
return nil, fmt.Errorf("json format does not support template option.")
}
return &JSONFormat{}, nil
case "template":
return &TemplateFormat{tmpl}, nil
}
return nil, fmt.Errorf("Unsupported format is specified.")
}
type JSONFormat struct{}
// TransformData returns JSON format string data.
func (p *JSONFormat) TransformData(data any) (string, error) {
var buf bytes.Buffer
err := codec.NewEncoder(&buf, jsonHandlePretty).Encode(data)
if err != nil {
return "", err
}
View on GitHub (pinned to 482b49bf1a)
Solutions
- Remove the -template flag when using json output.
- Or switch to -format=template (or drop -json) so the template is honored.
- If flags come from shared flag set, guard your script so only one of -json/-template is ever set.
- Update code calling DataFormat directly to pass "" as tmpl for json format.
Example fix
// before
f, err := command.DataFormat("json", myTmpl)
// after
f, err := command.DataFormat("json", "") // or command.DataFormat("template", myTmpl) Defensive patterns
Strategy: validation
Validate before calling
if format == "json" && len(tmpl) > 0 {
return errors.New("choose either json output or a template, not both")
} Type guard
null
Try / catch
f, err := command.DataFormat(format, tmpl)
if err != nil {
if strings.Contains(err.Error(), "does not support template") {
f, err = command.DataFormat("json", "")
}
} Prevention
- Make -json and -template mutually exclusive in your flag parsing (MarkFlagsMutuallyExclusive in cobra).
- Default to json when no template is given.
- Document flag exclusivity in command help text.
- Add unit tests asserting the rejection of combined options.
When it happens
Trigger: Calling command.DataFormat("json", tmpl) with len(tmpl) > 0, e.g. via the CLI when both -json (or format=json) and -template flags are set.
Common situations: Users passing both -json and -template flags to a Consul CLI command; scripts that hardcode format=json while a template flag is inherited from shared flag parsing code.
Related errors
- Both json and template formatting are not allowed
- format error: %v
- template needs to be specified in golang's text/template for
- default auth config text could not be deserialized: %v
- [✘] Role data could not be deserialized: %w
AI-assisted analysis of hashicorp/nomad@482b49bf1a (2026-09-04).
Data as JSON: /api/errors/96a904643a7b8d16.
Report an issue: GitHub.