JSON Stringify Helpers
Utilities for stable JSON stringification and cleaning surrounding JSON garbage.
stableStringify
Creates a deterministic JSON string representation of any value with guaranteed sorted object keys.
Function Signature
function stableStringify(obj: unknown): stringParameters
| Parameter | Type | Description |
|---|---|---|
obj | unknown | The value to convert to a stable JSON string. |
Returns
string: A deterministic JSON string with alphabetically sorted keys.
Example Usage
playground.ts
stripJsonEdgeGarbage
Removes leading or trailing garbage characters from strings that wrap JSON objects or arrays.
Function Signature
function stripJsonEdgeGarbage(str: string): stringParameters
| Parameter | Type | Description |
|---|---|---|
str | string | The input string containing a JSON payload with potential surrounding garbage. |
Returns
string: A sanitized string containing only the JSON object or array content.
Example Usage
playground.ts
Behavior Details
- Stable Stringification: Uses deep recursion to sort all nested objects' keys. This is critical for hash generation, digital signatures, caching keys, or performing reliable deep equality checks.
- Edge Garbage Cleanup: Scans the input string to locate the outermost boundaries (
{}or[]) and extracts the content between them. If no JSON structure boundaries are found, it returns the original string.
Last updated: Sun, Jun 14, 2026 07:06:16AM (UTC)
