{"record":{"id":"107b6e2c327be7e2","repo":"micro/go-micro","slug":"failed-to-unmarshal-object","errorCode":null,"errorMessage":"Failed to unmarshal object","messagePattern":"Failed to unmarshal object","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"store/nats-js-kv/nats.go","lineNumber":402,"sourceCode":"\t}\n\n\tn.buckets.Set(kv.Bucket, store)\n\n\treturn store, nil\n}\n\n// getRecord returns the record with the given key from the nats kv store.\nfunc (n *natsStore) getRecord(bucket nats.KeyValue, key string) (*store.Record, bool, error) {\n\tobj, err := bucket.Get(key)\n\tif errors.Is(err, nats.ErrKeyNotFound) {\n\t\treturn nil, false, store.ErrNotFound\n\t} else if err != nil {\n\t\treturn nil, false, errors.Wrap(err, \"Failed to get object from bucket\")\n\t}\n\n\tvar kv KeyValueEnvelope\n\tif err := json.Unmarshal(obj.Value(), &kv); err != nil {\n\t\treturn nil, false, errors.Wrap(err, \"Failed to unmarshal object\")\n\t}\n\n\tif obj.Operation() != nats.KeyValuePut {\n\t\treturn nil, false, nil\n\t}\n\n\treturn &store.Record{\n\t\tKey:      kv.Key,\n\t\tValue:    kv.Data,\n\t\tMetadata: kv.Metadata,\n\t}, true, nil\n}\n\nfunc (n *natsStore) natsKeys(bucket nats.KeyValue, table, key string, prefix, suffix bool) ([]string, error) {\n\tif !suffix && !prefix {\n\t\treturn []string{n.NatsKey(table, key)}, nil\n\t}\n","sourceCodeStart":384,"sourceCodeEnd":420,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/store/nats-js-kv/nats.go#L384-L420","documentation":"Returned by getRecord when json.Unmarshal of the value stored in the KV entry into KeyValueEnvelope fails. The plugin stores records as JSON envelopes ({key,data,metadata}), so any value in the bucket not written by this plugin (or corrupted) cannot be read.","triggerScenarios":"Calling store.Read(key) on a bucket entry that was written directly via NATS (nats kv put / another application) rather than through this store's Write, or whose envelope bytes were corrupted or truncated.","commonSituations":"Mixing go-micro store plugin access with manual 'nats kv put' writes of raw values; reading a bucket populated by a different tool with a different serialization format; manual edits or partial writes to the JetStream stream.","solutions":["Write all data through store.Write so entries are valid KeyValueEnvelope JSON","If manual values exist in the bucket, re-ingress them: read raw and rewrite with store.Write (json envelope {\"key\":...,\"data\":...,\"metadata\":...})","Check that the value is not empty/corrupted: 'nats kv get <bucket> <key>' and validate the JSON","Use a separate bucket for data written by other tools instead of sharing one"],"exampleFix":"// before\nnats kv put mybucket mykey --val \"raw string\"\nst.Read(\"mykey\") // -> Failed to unmarshal object\n// after\nst.Write(&store.Record{Key: \"mykey\", Value: []byte(\"raw string\")})\nst.Read(\"mykey\") // ok","handlingStrategy":"validation","validationCode":"raw, err := json.Marshal(rec)\n_ = raw\nif json.Valid([]byte(raw)) { /* will be stored as envelope by Write */ }","typeGuard":"func isValidEnvelope(b []byte) bool {\n    var kv KeyValueEnvelope\n    return json.Unmarshal(b, &kv) == nil\n}","tryCatchPattern":"_, err := st.Read(key)\nif err != nil && strings.Contains(err.Error(), \"Failed to unmarshal object\") {\n    // value was written outside this plugin; re-ingest or delete it\n    return fmt.Errorf(\"bucket contains foreign data at key %s\", key)\n}","preventionTips":["Never write raw values into the bucket with 'nats kv put' or other clients; use store.Write","Use separate buckets for data owned by other tools","Validate stored values are JSON envelopes after any manual intervention","Restrict bucket access to services using this store plugin"],"tags":["json","deserialization","kv","nats"],"backgroundTag":"json-unmarshal-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}