JuliusBrussee/caveman · error

generic decode: expected a JSON array or

Error message

generic decode: expected a JSON array or {"data":[...]} of objects

What it means

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.

Solutions

  1. Verify the payload is valid JSON (run it through jq or json.Unmarshal) before importing
  2. Wrap the records as a bare JSON array of objects: [{...},{...}]
  3. Or use the envelope shape {"data":[{...},{...}]} with the exact key `data`
  4. Ensure each element of the array is a JSON object, not a scalar or nested array
  5. Confirm you are not accidentally passing CSV/JSONL output of another exporter to the generic format

Example fix

// before (envelope key the generic importer does not know)
{"observations":[{"id":"1"}]}
// after
{"data":[{"id":"1"}]}
Defensive patterns

Strategy: validation

Validate before calling

func isGenericPayload(data []byte) bool {
  var arr []map[string]any
  if err := json.Unmarshal(data, &arr); err == nil { return true }
  var env struct{ Data []map[string]any `json:"data"` }
  return json.Unmarshal(data, &env) == nil && env.Data != nil
}

Type guard

func asRecordArray(v any) ([]map[string]any, bool) {
  arr, ok := v.([]any)
  if !ok { return nil, false }
  out := make([]map[string]any, 0, len(arr))
  for _, e := range arr { m, ok := e.(map[string]any); if !ok { return nil, false }; out = append(out, m) }
  return out, true
}

Try / catch

rows, err := importers.Parse(data, opts)
if err != nil {
  if strings.Contains(err.Error(), "generic decode: expected a JSON array") {
    // surface payload-shape problem to user; do not retry
  }
  return err
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/b564505c7555bd65. Report an issue: GitHub.

Appendix: source

Thrown at shared/platform/importers/generic.go:123

		}
	}
	return nil
}

func decodeGenericRecords(data []byte) ([]map[string]any, error) {
	// Try a bare array first.
	var arr []map[string]any
	if err := json.Unmarshal(data, &arr); err == nil {
		return arr, nil
	}
	// Then a {"data":[...]} wrapper.
	var env struct {
		Data []map[string]any `json:"data"`
	}
	if err := json.Unmarshal(data, &env); err == nil && env.Data != nil {
		return env.Data, nil
	}
	return nil, fmt.Errorf("generic decode: expected a JSON array or {\"data\":[...]} of objects")
}

func mapGeneric(rec map[string]any, opts Options) (Span, map[string]bool, error) {
	var sp Span
	startNs, endNs := int64(0), int64(0)
	resolved := make(map[string]bool, len(opts.FieldMap))
	for target, path := range opts.FieldMap {
		kind := genericTargets[target]
		val, found := lookupPath(rec, path)
		if !found {
			if target == "trace_id" || target == "span_id" || target == "timestamp" {
				return Span{}, nil, fmt.Errorf("mapped source path %q for required target %q was not found", path, target)
			}
			continue
		}
		resolved[target] = true
		switch kind {
		case "string":

View on GitHub (pinned to 3ee70a1026)