Parse JSON To Object
Safely parses a JSON string into an object with optional primitive value conversion.
parseJsonToObject
The parseJsonToObject function safely parses a JSON string into a JavaScript object. It optionally converts stringified primitive values (like "true", "42") into their actual primitive types (true, 42).
Function Signature
function parseJsonToObject<T extends GenericObject = GenericObject>(
value: string,
parsePrimitives?: boolean
): TParameters
value(string): The JSON string to parse.parsePrimitives(boolean, optional): Whentrue, automatically parses string values into primitives. Defaults totrue.
Return Value
T: The parsed object. If parsing fails, or if the parsed root is not a plain object, it returns an empty object{}.
Example Usage
playground.ts
Notes
- This function validates that the parsed value is a plain object. Roots like strings, numbers, arrays, or
nullare rejected and return{}. - Internally uses
parseObjectValuesto perform recursive primitive conversions on the object.
See Also
- parseObjectValues - The utility used to convert object property values.
- parseJSON - For parsing general JSON (which can return arrays, numbers, booleans, or null).
Last updated: Sun, Jun 14, 2026 07:06:16AM (UTC)
