{"record":{"id":"b15f47a9580d3f3f","repo":"hashicorp/nomad","slug":"error-unmarshaling-json-w","errorCode":null,"errorMessage":"error unmarshaling json: %w","messagePattern":"error unmarshaling json: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"command/var_put.go","lineNumber":423,"sourceCode":"}\n\n// makeVariable creates a variable based on whether or not there is data in\n// content and the format is set.\nfunc (c *VarPutCommand) makeVariable(path string) (*api.Variable, error) {\n\tvar err error\n\tout := new(api.Variable)\n\tif len(c.contents) == 0 {\n\t\tout.Path = path\n\t\tout.Namespace = c.Meta.namespace\n\t\tout.Items = make(map[string]string)\n\t\treturn out, nil\n\t}\n\n\tswitch c.inFmt {\n\tcase \"json\":\n\t\terr = json.Unmarshal(c.contents, out)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"error unmarshaling json: %w\", err)\n\t\t}\n\tcase \"hcl\":\n\t\tout, err = parseVariableSpec(c.contents, c.verbose)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"error parsing hcl: %w\", err)\n\t\t}\n\tcase \"\":\n\t\treturn nil, errors.New(\"format flag required\")\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"unknown format flag value\")\n\t}\n\n\t// It is possible a specification file was used which did not declare any\n\t// items. Therefore, default the entry to avoid panics and ensure this type\n\t// of use is valid.\n\tif out.Items == nil {\n\t\tout.Items = make(map[string]string)\n\t}","sourceCodeStart":405,"sourceCodeEnd":441,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/command/var_put.go#L405-L441","documentation":"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>`.","triggerScenarios":"`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).","commonSituations":"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.","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."],"exampleFix":"// before (invalid: trailing comma)\n{ \"path\": \"app/config\", \"items\": { \"k\": \"v\", }, }\n// after\n{ \"path\": \"app/config\", \"items\": { \"k\": \"v\" } }","handlingStrategy":"validation","validationCode":"var v any\nif err := json.Unmarshal(data, &v); err != nil {\n    return fmt.Errorf(\"spec is not valid JSON: %w\", err)\n}\nif _, ok := v.(map[string]any); !ok {\n    return fmt.Errorf(\"spec root must be a JSON object\")\n}","typeGuard":"func isJSONObject(data []byte) bool {\n    var v map[string]any\n    return json.Unmarshal(data, &v) == nil && v != nil\n}","tryCatchPattern":"if err := run(); err != nil {\n    var uerr *json.UnmarshalTypeError\n    if errors.As(err, &uerr) {\n        log.Fatalf(\"field %s: expected %s\", uerr.Field, uerr.Type)\n    }\n    log.Fatal(err)\n}","preventionTips":["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."],"tags":["json","cli","nomad","unmarshaling"],"backgroundTag":"json-parse-error","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"}