{"record":{"id":"037c4474ffe5f860","repo":"VictoriaMetrics/VictoriaMetrics","slug":"cannot-unmarshal-q-w","errorCode":null,"errorMessage":"cannot unmarshal %q: %w","messagePattern":"cannot unmarshal %q: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/protoparser/datadogv1/parser.go","lineNumber":33,"sourceCode":"func (req *Request) reset() {\n\t// recursively reset all the fields in req in order to avoid field value\n\t// reuse in json.Unmarshal() when the corresponding field is missing\n\t// in the unmarshaled JSON.\n\t// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3432\n\tseries := req.Series\n\tfor i := range series {\n\t\tseries[i].reset()\n\t}\n\treq.Series = series[:0]\n}\n\n// Unmarshal unmarshals DataDog /api/v1/series request body from b to req.\n//\n// b shouldn't be modified when req is in use.\nfunc (req *Request) Unmarshal(b []byte) error {\n\treq.reset()\n\tif err := json.Unmarshal(b, req); err != nil {\n\t\treturn fmt.Errorf(\"cannot unmarshal %q: %w\", b, err)\n\t}\n\t// Set missing timestamps to the current time.\n\tcurrentTimestamp := float64(fasttime.UnixTimestamp())\n\tseries := req.Series\n\tfor i := range series {\n\t\tpoints := series[i].Points\n\t\tfor j := range points {\n\t\t\tif points[j][0] <= 0 {\n\t\t\t\tpoints[j][0] = currentTimestamp\n\t\t\t}\n\t\t}\n\t}\n\treturn nil\n}\n\n// Series represents a series item from DataDog POST request to /api/v1/series\ntype Series struct {\n\tMetric string `json:\"metric\"`","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/VictoriaMetrics/VictoriaMetrics/blob/5079fb58f1e8e62113f90c945ad71586c797d770/lib/protoparser/datadogv1/parser.go#L15-L51","documentation":"Request.Unmarshal decodes a DataDog /api/v1/series JSON body with encoding/json; if json.Unmarshal fails, the raw body and cause are wrapped in 'cannot unmarshal %q'. Typical causes are non-JSON bodies, malformed JSON, or wrong JSON shapes (e.g. points not [[ts,val]] arrays).","triggerScenarios":"Client POSTs to the DataDog v1 import endpoint with a body that fails strict JSON decoding into the Request struct: invalid JSON syntax, unexpected types (string where number expected), or empty body.","commonSituations":"Configuring a DogStatsD or protobuf-sketch client against the v1 JSON endpoint, hand-written scripts posting malformed JSON, template/interpolation bugs producing invalid payloads, proxies mangling the body.","solutions":["Validate the posted body with jq (jq . < body.json) to see JSON syntax errors","Check the wrapped snippet %q shows what bytes actually arrived — often reveals wrong endpoint or mangling","Fix the field types to match the /api/v1/series schema (points: [[number, number], ...])","Send protobuf sketches to the sketches endpoint instead if the sender is a Datadog agent using v2"],"exampleFix":"// before\n// body: {\"series\":[{\"metric\":\"m\",\"points\":[\"1,2\"]}]}\n// error: cannot unmarshal \"...\": json: cannot unmarshal string into ...Points\n// after\n{\"series\":[{\"metric\":\"m\",\"points\":[[1696000000,2]]}]}","handlingStrategy":"validation","validationCode":"// Validate v1 series JSON shape before POSTing\nfunc validV1(body []byte) bool {\n    var req struct {\n        Series []struct {\n            Metric string     `json:\"metric\"`\n            Points [][2]float64 `json:\"points\"`\n        } `json:\"series\"`\n    }\n    return json.Unmarshal(body, &req) == nil && len(req.Series) > 0\n}\nif !validV1(body) { return errors.New(\"not a valid /api/v1/series JSON body\") }","typeGuard":"func isDatadogV1JSON(body []byte) bool {\n    var probe map[string]json.RawMessage\n    if err := json.Unmarshal(body, &probe); err != nil { return false }\n    _, ok := probe[\"series\"]\n    return ok\n}","tryCatchPattern":"req := &datadogv1.Request{}\nif err := req.Unmarshal(body); err != nil {\n    log.Printf(\"v1 unmarshal failed for body %.200q: %v\", body, err)\n    http.Error(w, \"bad request\", http.StatusBadRequest)\n}","preventionTips":["Generate payloads with json.Marshal from typed structs, never string templates","Validate JSON with jq before deploying scripts that post to /api/v1/series","Use points as [[timestamp, value], ...] numeric pairs","Send protobuf sketches to the v2 endpoint instead when using the Datadog agent"],"tags":["datadog","json","unmarshal","parsing"],"backgroundTag":"json-unmarshal-failed","analyzedSha":"5079fb58f1e8e62113f90c945ad71586c797d770","analyzedAt":"2026-09-03T18:10:26.153Z","contentChangedAt":"2026-09-03T18:10:26.153Z","schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}