larksuite/cli · error

expected object, got %v

Error message

expected object, got %v

What it means

OrderedMap.UnmarshalJSON uses a streaming json.Decoder and requires the input to begin with a '{' object token so it can preserve key order. If the first token is anything else (array, string, number, boolean, null), this error is returned.

Source

Thrown at internal/schema/types.go:156

		if err != nil {
			return nil, fmt.Errorf("marshal value for %q: %w", k, err)
		}
		buf.Write(valJSON)
	}
	buf.WriteByte('}')
	return buf.Bytes(), nil
}

// UnmarshalJSON parses an object preserving key order via json.Decoder.Token().
// Used for round-tripping in tests (and future golden update flows).
func (o *OrderedProps) UnmarshalJSON(data []byte) error {
	dec := json.NewDecoder(bytes.NewReader(data))
	tok, err := dec.Token()
	if err != nil {
		return err
	}
	if delim, ok := tok.(json.Delim); !ok || delim != '{' {
		return fmt.Errorf("expected object, got %v", tok)
	}
	o.Order = nil
	o.Map = make(map[string]Property)
	for dec.More() {
		keyTok, err := dec.Token()
		if err != nil {
			return err
		}
		key, ok := keyTok.(string)
		if !ok {
			return fmt.Errorf("expected string key, got %v", keyTok)
		}
		var prop Property
		if err := dec.Decode(&prop); err != nil {
			return err
		}
		o.Order = append(o.Order, key)
		o.Map[key] = prop

View on GitHub (pinned to 7fd6ef3c07)

Solutions

  1. Ensure the JSON input is an object starting with '{'
  2. Log/inspect the raw payload before unmarshaling to verify its shape
  3. Pre-validate the first non-whitespace byte is '{' before calling Decode

Example fix

// before
data := []byte(`[]`)
json.Unmarshal(data, &om) // expected object, got [
// after
data := []byte(`{"a":{}}`)
json.Unmarshal(data, &om)
Defensive patterns

Strategy: validation

Validate before calling

trimmed := bytes.TrimLeft(raw, " \t\r\n")
if len(trimmed) == 0 || trimmed[0] != '{' {
	return errors.New("payload is not a JSON object")
}
err := json.Unmarshal(raw, &om)

Try / catch

if err := json.Unmarshal(data, &om); err != nil {
	if strings.Contains(err.Error(), "expected object") {
		// payload is not an object: log raw data and recover with defaults
	}
	return err
}

Prevention

When it happens

Trigger: Calling json.Unmarshal into an OrderedMap (or Property containing one) with JSON input like "[]", "\"str\"", "123", "true", or "null" instead of an object.

Common situations: API response shape changed from object to array or scalar; feeding a JSON fragment into the wrong field; unmarshaling an empty/null payload where an object schema was expected.

Related errors


AI-assisted analysis of larksuite/cli@7fd6ef3c07 (2026-09-04). Data as JSON: /api/errors/a9477079b3989cc0. Report an issue: GitHub.