larksuite/cli · error

invalid JSON trailing content: %w

Error message

invalid JSON trailing content: %w

What it means

When the second decoder pass fails with a non-nil, non-EOF error, the input had one valid JSON value followed by unparseable trailing bytes; it is reported as 'invalid JSON trailing content: %w' wrapping the decoder error.

Source

Thrown at shortcuts/common/typed_binder.go:182

			if err != nil {
				return nil, err
			}
			text = string(encoded)
		}
		value := reflect.New(field.valueType)
		decoder := json.NewDecoder(strings.NewReader(text))
		if objectShape, ok := shapeAsObject(field.shape); ok && !objectShape.AdditionalProperties {
			decoder.DisallowUnknownFields()
		}
		if err := decoder.Decode(value.Interface()); err != nil {
			return nil, fmt.Errorf("invalid JSON: %w", err)
		}
		var trailing any
		if err := decoder.Decode(&trailing); err != io.EOF {
			if err == nil {
				return nil, fmt.Errorf("invalid JSON: multiple values")
			}
			return nil, fmt.Errorf("invalid JSON trailing content: %w", err)
		}
		return value.Elem().Interface(), nil
	}
	return convertReflectValue(raw, field.valueType)
}

func convertReflectValue(raw any, target reflect.Type) (any, error) {
	if raw == nil {
		return nil, nil
	}
	rawValue := reflect.ValueOf(raw)
	if rawValue.Type().AssignableTo(target) {
		return raw, nil
	}
	if target == jsonRawMessageType {
		encoded, err := json.Marshal(raw)
		if err != nil {
			return nil, err

View on GitHub (pinned to 7fd6ef3c07)

Solutions

  1. Remove everything after the closing brace/bracket of the JSON value
  2. Check the source file for trailing junk (cat -A file) and trim it
  3. Pipe through jq to normalize: jq -c . file

Example fix

// before
--data '{"a":1} // done'
// after
--data '{"a":1}'
Defensive patterns

Strategy: validation

Validate before calling

JSON.parse(text.trim()) // in shell: jq -e . <<< "$DATA" >/dev/null

Type guard

func hasTrailingContent(s string) bool { d := json.NewDecoder(strings.NewReader(s)); var v any; if d.Decode(&v) != nil { return false }; return d.Decode(&struct{}{}) != io.EOF }

Try / catch

if strings.Contains(err.Error(), "trailing content") { /* inspect raw input for junk after the JSON value */ }

Prevention

When it happens

Trigger: Trailing characters after valid JSON, e.g. --data '{"a":1} extra', stray text appended by a shell redirect, or a BOM/whitespace-embedded comment after the JSON body.

Common situations: Appending notes after JSON in scripts; editor saving extra content; $(cat f) expansions concatenating files.

Understand the failure class

Related errors


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