Open-source document signature platform. Not affiliated with DocuSign.
TypeScript
/**
* JSON with object keys in sorted order and undefined members left out, so
* a value hashes the same before it is stored and after Postgres hands it
* back from a jsonb column (which keeps neither key order nor undefined).
*/
export function canonicalJson(value: unknown): string {
if (Array.isArray(value)) {
return `[${value.map(canonicalJson).join(",")}]`;
}
if (value && typeof value === "object") {
const record = value as Record<string, unknown>;
return `{${Object.keys(record)
.filter((key) => record[key] !== undefined)
.sort()
.map((key) => `${JSON.stringify(key)}:${canonicalJson(record[key])}`)
.join(",")}}`;
}
return JSON.stringify(value ?? null);
}