getsops/sops · error
Could not unmarshal input data: %s
Error message
Could not unmarshal input data: %s
What it means
This error wraps any failure that occurs while parsing plaintext JSON input bytes into a sops.TreeBranches structure inside the JSON Store. The JSON store's LoadPlainFile feeds the raw bytes to treeBranchFromJSON, which uses encoding/json to decode into a tree; if the input is not valid JSON (or violates JSON value shapes), the underlying decoder error is surfaced wrapped with this message.
Source
Thrown at stores/json/store.go:338
return sops.Tree{}, err
}
branches, metadata, err := stores.ExtractMetadata(branches, stores.MetadataOpts{
Flatten: stores.MetadataFlattenNone,
})
if err != nil {
return sops.Tree{}, err
}
return sops.Tree{
Branches: branches,
Metadata: metadata,
}, nil
}
// LoadPlainFile loads plaintext json file bytes onto a sops.TreeBranches object
func (store *Store) LoadPlainFile(in []byte) (sops.TreeBranches, error) {
branch, err := store.treeBranchFromJSON(in)
if err != nil {
return nil, fmt.Errorf("Could not unmarshal input data: %s", err)
}
return sops.TreeBranches{
branch,
}, nil
}
// EmitEncryptedFile returns the encrypted bytes of the json file corresponding to a
// sops.Tree runtime object
func (store *Store) EmitEncryptedFile(in sops.Tree) ([]byte, error) {
branches, err := stores.SerializeMetadata(in, stores.MetadataOpts{
Flatten: stores.MetadataFlattenNone,
})
if err != nil {
return nil, fmt.Errorf("Error marshaling metadata: %s", err)
}
return store.EmitPlainFile(branches)
}
View on GitHub (pinned to 13442bb981)
Solutions
- Validate the input with a JSON parser (e.g. `jq . file`) to see the exact syntax error location
- Ensure the correct sops format/store is used: match --input-type/-i to the real file format (yaml, ini, env, json)
- Strip BOM and encoding issues: re-save the file as clean UTF-8 without BOM
- Fix the reported JSON syntax error in the file (the wrapped %s contains the Go json error with offset)
Example fix
// before: wrong store for content store := json.NewStore() tree, err := store.LoadPlainFile(yamlBytes) // "Could not unmarshal input data" // after: use the store matching the content store := yaml.NewStore() tree, err := store.LoadPlainFile(yamlBytes)
Defensive patterns
Strategy: validation
Validate before calling
func validateJSON(b []byte) error {
b = bytes.TrimPrefix(b, []byte{0xEF, 0xBB, 0xBF}) // strip BOM
if len(bytes.TrimSpace(b)) == 0 {
return errors.New("empty input")
}
var v interface{}
return json.Unmarshal(b, &v)
}
if err := validateJSON(input); err != nil {
return fmt.Errorf("not valid JSON: %w", err)
}
tree, err := store.LoadPlainFile(input) Try / catch
tree, err := store.LoadPlainFile(data)
if err != nil {
if strings.HasPrefix(err.Error(), "Could not unmarshal input data") {
return fmt.Errorf("input is not valid JSON: %w", err)
}
return err
} Prevention
- Validate input with jq or json.Unmarshal before calling sops store APIs
- Match the sops --input-type/store to the actual file format
- Save files as UTF-8 without BOM
- Never feed binary or non-JSON data to the JSON store
When it happens
Trigger: Calling LoadPlainFile (or any higher-level path like LoadPlainData/decrypt workflows using the JSON store) with bytes that are not well-formed JSON: malformed syntax, trailing commas, comments, BOM bytes, empty input, or non-JSON binary data passed to the JSON store.
Common situations: Encrypting a file with the wrong format detection (e.g. a YAML file being processed by the JSON store), a .env or INI file passed to sops with --input-type json, corrupted downloads/partial writes of config files, or files starting with a UTF-8 BOM.
Understand the failure class
Background: "failed to unmarshal" / json.Unmarshal errors: why parsing a response into a Go struct fails and how to fix it — this error's family across 23 libraries.
Related errors
- failed to parse input as Bech32-encoded age public key: %w
- failed to parse input as age-ssh public key: %w
- Could not unmarshal config file: %s
- Could not unmarshal input data: %s
- Error marshaling to json: %s
AI-assisted analysis of getsops/sops@13442bb981 (2026-09-01).
Data as JSON: /api/errors/32fa7974577dfb18.
Report an issue: GitHub.