OpenNHP/opennhp · warning

json parsing error

Error message

json parsing error: %s

What it means

DataPrivateKeyStore.fromJson unmarshals raw JSON bytes into the DataPrivateKeyStore struct (fields dataPrivateKeyBase64, providerPublicKeyBase64). If the bytes are not valid JSON for this struct, json.Unmarshal fails and this wrapped error is returned. Note the current caller in NewDataPrivateKeyStoreWith discards this error (_ = d.fromJson(...)), so corrupted key files silently yield an empty store instead of surfacing this error.

Solutions

  1. Validate the file with `jq . <path>` or json.Valid(fileContentByte) to confirm the JSON is well-formed.
  2. Stop ignoring the error at endpoints/db/utils.go:51 - propagate the result of d.fromJson(fileContentByte) so corrupt key files fail loudly.
  3. Restore the key file from backup or regenerate it (Generate + Save) if it is corrupt; re-register the key with the provider.
  4. Ensure the file contains exactly one JSON object with dataPrivateKeyBase64 and providerPublicKeyBase64 fields.

Example fix

// before: parse error silently ignored
d = &DataPrivateKeyStore{}
_ = d.fromJson(fileContentByte)
return
// after: propagate parse failures
if err := d.fromJson(fileContentByte); err != nil {
	return nil, fmt.Errorf("invalid key file %s: %w", fullPath, err)
}
return
Defensive patterns

Strategy: validation

Validate before calling

content, err := os.ReadFile(path)
if err != nil { return err }
if !json.Valid(content) {
	return fmt.Errorf("%s is not valid JSON", path)
}

Type guard

func isValidKeyStoreJSON(b []byte) bool {
	var probe struct {
		DataPrivateKeyBase64    string `json:"dataPrivateKeyBase64"`
		ProviderPublicKeyBase64 string `json:"providerPublicKeyBase64"`
	}
	return json.Unmarshal(b, &probe) == nil
}

Try / catch

if err := store.FromJsonChecked(content); err != nil {
	return fmt.Errorf("corrupt key file; restore from backup or regenerate: %w", err)
}

Prevention

When it happens

Trigger: Calling fromJson with bytes that are: (1) not JSON at all (binary, empty file, HTML error page); (2) valid JSON but not an object with the expected fields (e.g. a JSON array or wrong schema); (3) truncated JSON from an interrupted Save/write.

Common situations: Key file corrupted by a crash or full disk during a previous write; user hand-edited the file and broke the JSON; wrong file passed in by mistake.

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


AI-assisted analysis of OpenNHP/opennhp@6e04ca5ff0 (2026-09-07). Data as JSON: /api/errors/69dd1080f85a4317. Report an issue: GitHub.

Appendix: source

Thrown at endpoints/db/utils.go:112

	if err != nil {
		return fmt.Errorf("failed to delete file: %v", err)
	}
	return nil
}

func (d *DataPrivateKeyStore) toJson() []byte {
	dataPrkStoreJson, err := json.Marshal(d)
	if err != nil {
		return []byte("{}")
	} else {
		return dataPrkStoreJson
	}
}

func (d *DataPrivateKeyStore) fromJson(jsonData []byte) error {
	err := json.Unmarshal(jsonData, d)
	if err != nil {
		return fmt.Errorf("json parsing error: %s", err)
	}
	return nil
}

type AppParams struct {
	Mode                    string // the mode of operation: none, encrypt and decrypt
	Source                  string // the path of plaintext data
	DsType                  string // the type of data source: stream, online and offline
	Output                  string // path of output file
	SmartPolicy             string // path of smart policy
	Metadata                string // path of metadata
	ZtdoFilePath            string // path of ztdo file when mode is decrypt
	ZtdoId                  string // identifier of ztdo file
	DataPrivateKeyBase64    string
	AccessUrl               string // path of access url of ztdo
	ProviderPublicKeyBase64 string
}

View on GitHub (pinned to 6e04ca5ff0)