{"record":{"id":"96a904643a7b8d16","repo":"hashicorp/nomad","slug":"json-format-does-not-support-template-option","errorCode":null,"errorMessage":"json format does not support template option.","messagePattern":"json format does not support template option\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"command/data_format.go","lineNumber":33,"sourceCode":"var (\n\tjsonHandlePretty = &codec.JsonHandle{\n\t\tHTMLCharsAsIs: true,\n\t\tIndent:        4,\n\t}\n)\n\n// DataFormatter is a transformer of the data.\ntype DataFormatter interface {\n\t// TransformData should return transformed string data.\n\tTransformData(any) (string, error)\n}\n\n// DataFormat returns the data formatter specified format.\nfunc DataFormat(format, tmpl string) (DataFormatter, error) {\n\tswitch format {\n\tcase \"json\":\n\t\tif len(tmpl) > 0 {\n\t\t\treturn nil, fmt.Errorf(\"json format does not support template option.\")\n\t\t}\n\t\treturn &JSONFormat{}, nil\n\tcase \"template\":\n\t\treturn &TemplateFormat{tmpl}, nil\n\t}\n\treturn nil, fmt.Errorf(\"Unsupported format is specified.\")\n}\n\ntype JSONFormat struct{}\n\n// TransformData returns JSON format string data.\nfunc (p *JSONFormat) TransformData(data any) (string, error) {\n\tvar buf bytes.Buffer\n\terr := codec.NewEncoder(&buf, jsonHandlePretty).Encode(data)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/command/data_format.go#L15-L51","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nf, err := command.DataFormat(\"json\", myTmpl)\n// after\nf, err := command.DataFormat(\"json\", \"\") // or command.DataFormat(\"template\", myTmpl)","handlingStrategy":"validation","validationCode":"if format == \"json\" && len(tmpl) > 0 {\n    return errors.New(\"choose either json output or a template, not both\")\n}","typeGuard":"null","tryCatchPattern":"f, err := command.DataFormat(format, tmpl)\nif err != nil {\n    if strings.Contains(err.Error(), \"does not support template\") {\n        f, err = command.DataFormat(\"json\", \"\")\n    }\n}","preventionTips":["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."],"tags":["cli","json","template"],"backgroundTag":"conflicting-output-format-options","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}