hashicorp/nomad · error
error unmarshaling json: %w
Error message
error unmarshaling json: %w
What it means
In `nomad var put` with `-in=json` (or a .json spec file), makeVariable unmarshals the file contents into api.Variable. If the bytes are not valid JSON, or do not match the expected Variable schema (e.g. top-level not an object, wrong types for fields), json.Unmarshal fails and the error is wrapped as `error unmarshaling json: <detail>`.
Source
Thrown at command/var_put.go:423
}
// makeVariable creates a variable based on whether or not there is data in
// content and the format is set.
func (c *VarPutCommand) makeVariable(path string) (*api.Variable, error) {
var err error
out := new(api.Variable)
if len(c.contents) == 0 {
out.Path = path
out.Namespace = c.Meta.namespace
out.Items = make(map[string]string)
return out, nil
}
switch c.inFmt {
case "json":
err = json.Unmarshal(c.contents, out)
if err != nil {
return nil, fmt.Errorf("error unmarshaling json: %w", err)
}
case "hcl":
out, err = parseVariableSpec(c.contents, c.verbose)
if err != nil {
return nil, fmt.Errorf("error parsing hcl: %w", err)
}
case "":
return nil, errors.New("format flag required")
default:
return nil, fmt.Errorf("unknown format flag value")
}
// It is possible a specification file was used which did not declare any
// items. Therefore, default the entry to avoid panics and ensure this type
// of use is valid.
if out.Items == nil {
out.Items = make(map[string]string)
}View on GitHub (pinned to 482b49bf1a)
Solutions
- Validate the file with `jq . file.json` (or a JSON linter) and fix syntax errors — comments and trailing commas are not allowed.
- Ensure the root of the document is a JSON object matching the Variable schema (keys like `path`, `items`, `create_index`).
- If the content is HCL, switch the flag: `nomad var put -in=hcl @file`.
- Generate a correct skeleton with `nomad var init` and edit from there.
Example fix
// before (invalid: trailing comma)
{ "path": "app/config", "items": { "k": "v", }, }
// after
{ "path": "app/config", "items": { "k": "v" } } Defensive patterns
Strategy: validation
Validate before calling
var v any
if err := json.Unmarshal(data, &v); err != nil {
return fmt.Errorf("spec is not valid JSON: %w", err)
}
if _, ok := v.(map[string]any); !ok {
return fmt.Errorf("spec root must be a JSON object")
} Type guard
func isJSONObject(data []byte) bool {
var v map[string]any
return json.Unmarshal(data, &v) == nil && v != nil
} Try / catch
if err := run(); err != nil {
var uerr *json.UnmarshalTypeError
if errors.As(err, &uerr) {
log.Fatalf("field %s: expected %s", uerr.Field, uerr.Type)
}
log.Fatal(err)
} Prevention
- Validate JSON with `jq .` or a linter before submitting.
- Generate specs from `nomad var init` instead of hand-writing.
- Remember JSON forbids comments and trailing commas.
When it happens
Trigger: `nomad var put ... -in=json @file` where the file contains truncated JSON, JSON5/HCL-style comments, trailing commas, a bare string/array at root, or field types that do not match api.Variable (e.g. `create_index` as a string instead of an int).
Common situations: Hand-edited spec files left with a trailing comma; exporting from another tool that emits YAML but naming the file .json; template-rendered JSON that failed to render valid output; copying an HCL example into a .json file.
Understand the failure class
Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.
Related errors
- format error: %v
- A template must be supplied using '-template' when using go-
- Invalid value for "-out"; valid values are [go-template, hcl
- Error listing host volumes
- -node or -node-pool options can only be used when no ID is p
AI-assisted analysis of hashicorp/nomad@482b49bf1a (2026-09-04).
Data as JSON: /api/errors/b15f47a9580d3f3f.
Report an issue: GitHub.