larksuite/cli · error
invalid JSON: %w
Error message
invalid JSON: %w
What it means
decodeCompiledValue parses a JSON-typed input field with encoding/json; when the text is not parseable as the field's target type, it wraps the decoder error as 'invalid JSON: %w'. Unknown object fields are also rejected when the field's schema disallows additionalProperties.
Source
Thrown at shortcuts/common/typed_binder.go:175
}
func decodeCompiledValue(raw any, field compiledInputField) (any, error) {
if field.cli.Encoding == typedEncodingJSON {
text, ok := raw.(string)
if !ok {
encoded, err := json.Marshal(raw)
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)View on GitHub (pinned to 7fd6ef3c07)
Solutions
- Validate the JSON with a parser (echo '<json>' | jq .) before passing it
- Use --flag='{"key":"value"}' with proper double quotes and escape shell metacharacters
- Match the target type exactly (numbers unquoted, strings quoted) and remove unknown fields
- Use @file or stdin JSON input if the shell quoting is too complex
Example fix
// before
--data "{'name':'x'}"
// after
--data '{"name":"x"}' Defensive patterns
Strategy: validation
Validate before calling
const fs = require('fs'); JSON.parse(fs.readFileSync('data.json','utf8')) // or: echo "$DATA" | jq -e . >/dev/null && lark-cli ... --data "$DATA" Type guard
func isJSONObject(s string) bool { return json.Valid([]byte(s)) && strings.HasPrefix(strings.TrimSpace(s), "{") } Try / catch
if err != nil && strings.HasPrefix(err.Error(), "invalid JSON") { log.Fatalf("bad --data value: %v", err) } Prevention
- Validate JSON with jq or a linter before passing it
- Prefer @file or stdin input for complex JSON
- Double-quote flag values and escape for your shell
- Match the schema's field names and types exactly
When it happens
Trigger: Passing malformed JSON to a JSON-encoded input flag, e.g. --data '{a:1}' (unquoted keys), trailing commas, single quotes, or a type mismatch like a string where the target struct expects a number; unknown fields when the schema has AdditionalProperties=false.
Common situations: Shell quoting mangling double quotes; hand-written JSON with JS-style syntax; schema drift after the API added/renamed fields the user still sends.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- invalid JSON: multiple values
- invalid JSON trailing content: %w
- lark-cli stdout was not JSON: {snippet}
- lark-cli returned a non-object JSON payload
- malformed config
AI-assisted analysis of larksuite/cli@7fd6ef3c07 (2026-09-04).
Data as JSON: /api/errors/cf38061cdd944706.
Report an issue: GitHub.