{"record":{"id":"b564505c7555bd65","repo":"JuliusBrussee/caveman","slug":"generic-decode-expected-a-json-array-or-data","errorCode":null,"errorMessage":"generic decode: expected a JSON array or {\"data\":[...]} of objects","messagePattern":"generic decode: expected a JSON array or (.+?) of objects","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared/platform/importers/generic.go","lineNumber":123,"sourceCode":"\t\t}\n\t}\n\treturn nil\n}\n\nfunc decodeGenericRecords(data []byte) ([]map[string]any, error) {\n\t// Try a bare array first.\n\tvar arr []map[string]any\n\tif err := json.Unmarshal(data, &arr); err == nil {\n\t\treturn arr, nil\n\t}\n\t// Then a {\"data\":[...]} wrapper.\n\tvar env struct {\n\t\tData []map[string]any `json:\"data\"`\n\t}\n\tif err := json.Unmarshal(data, &env); err == nil && env.Data != nil {\n\t\treturn env.Data, nil\n\t}\n\treturn nil, fmt.Errorf(\"generic decode: expected a JSON array or {\\\"data\\\":[...]} of objects\")\n}\n\nfunc mapGeneric(rec map[string]any, opts Options) (Span, map[string]bool, error) {\n\tvar sp Span\n\tstartNs, endNs := int64(0), int64(0)\n\tresolved := make(map[string]bool, len(opts.FieldMap))\n\tfor target, path := range opts.FieldMap {\n\t\tkind := genericTargets[target]\n\t\tval, found := lookupPath(rec, path)\n\t\tif !found {\n\t\t\tif target == \"trace_id\" || target == \"span_id\" || target == \"timestamp\" {\n\t\t\t\treturn Span{}, nil, fmt.Errorf(\"mapped source path %q for required target %q was not found\", path, target)\n\t\t\t}\n\t\t\tcontinue\n\t\t}\n\t\tresolved[target] = true\n\t\tswitch kind {\n\t\tcase \"string\":","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/importers/generic.go#L105-L141","documentation":"The generic importer decodes the input payload as either a bare JSON array of objects or an envelope object with a `data` key holding that array. This error is thrown when the payload matches neither shape, so no records can be extracted. It is fail-closed by design: the importer refuses to guess rather than silently ingest nothing.","triggerScenarios":"Calling importers.Parse with FormatGeneric on data that is not valid JSON at all, is a JSON object without a `data` array key, is a JSON primitive/string/number, or where `data` holds non-objects (e.g. an array of arrays or strings).","commonSituations":"Pasting the wrong file into the generic importer (e.g. CSV or JSONL instead of a JSON array); passing a Helicone/Langfuse-style envelope with a different wrapper key like `observations`; an upstream API returning a {\"error\": ...} body that gets fed straight to the importer; truncated or double-encoded JSON.","solutions":["Verify the payload is valid JSON (run it through jq or json.Unmarshal) before importing","Wrap the records as a bare JSON array of objects: [{...},{...}]","Or use the envelope shape {\"data\":[{...},{...}]} with the exact key `data`","Ensure each element of the array is a JSON object, not a scalar or nested array","Confirm you are not accidentally passing CSV/JSONL output of another exporter to the generic format"],"exampleFix":"// before (envelope key the generic importer does not know)\n{\"observations\":[{\"id\":\"1\"}]}\n// after\n{\"data\":[{\"id\":\"1\"}]}","handlingStrategy":"validation","validationCode":"func isGenericPayload(data []byte) bool {\n  var arr []map[string]any\n  if err := json.Unmarshal(data, &arr); err == nil { return true }\n  var env struct{ Data []map[string]any `json:\"data\"` }\n  return json.Unmarshal(data, &env) == nil && env.Data != nil\n}","typeGuard":"func asRecordArray(v any) ([]map[string]any, bool) {\n  arr, ok := v.([]any)\n  if !ok { return nil, false }\n  out := make([]map[string]any, 0, len(arr))\n  for _, e := range arr { m, ok := e.(map[string]any); if !ok { return nil, false }; out = append(out, m) }\n  return out, true\n}","tryCatchPattern":"rows, err := importers.Parse(data, opts)\nif err != nil {\n  if strings.Contains(err.Error(), \"generic decode: expected a JSON array\") {\n    // surface payload-shape problem to user; do not retry\n  }\n  return err\n}","preventionTips":["Validate payload shape with jq/JSON schema before import","Only feed JSON arrays or {\"data\":[...]} envelopes to the generic importer","Route other envelope shapes to their dedicated importer format","Fail fast on non-JSON files at ingestion boundaries"],"tags":["json","import","validation"],"backgroundTag":"unexpected-response-shape","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}