hashicorp/nomad · error

Both json and template formatting are not allowed

Error message

Both json and template formatting are not allowed

What it means

Format (command/data_format.go) is the CLI-level helper that maps the boolean -json flag and -template string into a DataFormat call. It rejects the mutually exclusive combination of json=true and a non-empty template, since output can only be rendered one way.

Source

Thrown at command/data_format.go:81

		return "", fmt.Errorf("template needs to be specified in golang's text/template format.")
	}

	t, err := template.New("").Funcs(makeFuncMap()).Parse(p.tmpl)
	if err != nil {
		return "", err
	}

	err = t.Execute(&out, data)
	if err != nil {
		return "", err
	}
	return out.String(), nil
}

func Format(json bool, template string, data any) (string, error) {
	var format string
	if json && len(template) > 0 {
		return "", fmt.Errorf("Both json and template formatting are not allowed")
	} else if json {
		format = "json"
	} else if len(template) > 0 {
		format = "template"
	} else {
		return "", fmt.Errorf("no formatting option given")
	}

	f, err := DataFormat(format, template)
	if err != nil {
		return "", err
	}

	out, err := f.TransformData(data)
	if err != nil {
		return "", fmt.Errorf("Error formatting the data: %w", err)
	}

View on GitHub (pinned to 482b49bf1a)

Solutions

  1. Pass only one formatting option: either json=true or a non-empty template.
  2. Fix wrapper scripts so -template is dropped when -json is present (or vice versa).
  3. If you need json filtered through a template, use format=template and pipe data through jq instead.
  4. Add CLI-level flag validation to fail fast with a clear usage message.

Example fix

// before
out, err := command.Format(true, tmpl, data) // both set
// after
out, err := command.Format(len(tmpl) == 0, tmpl, data) // or drop one option
Defensive patterns

Strategy: validation

Validate before calling

if json && len(template) > 0 {
    return errors.New("-json and -template are mutually exclusive")
}

Type guard

null

Try / catch

out, err := command.Format(jsonFlag, tmplFlag, data)
if err != nil {
    if strings.Contains(err.Error(), "Both json and template") {
        return usageError("pass either -json or -template, not both")
    }
    return err
}

Prevention

When it happens

Trigger: Calling command.Format(true, tmpl, data) with len(tmpl) > 0 — e.g. a CLI invocation that sets both -json and -template.

Common situations: Users passing both -json and -template to commands like consul services list or catalog commands; wrapper scripts that always append -json while also injecting a template; migration from old flag combinations after this validation was added.

Related errors


AI-assisted analysis of hashicorp/nomad@482b49bf1a (2026-09-04). Data as JSON: /api/errors/f8f34c070c8aae63. Report an issue: GitHub.