Parse Object Values
Recursively converts stringified primitive values within objects and arrays to their proper types (boolean, number, null, etc.).
parseObjectValues
The parseObjectValues function recursively scans an object or array and converts stringified primitive values (e.g., "true", "42", "null", "undefined") or valid JSON strings into their actual JavaScript types.
Function Signature
function parseObjectValues<
T extends GenericObject,
ParseNested extends boolean = true,
Result extends { [K in keyof T]: Any; } = ParsedObjectValues<T, ParseNested>
>(object: T, parseNested?: ParseNested): ResultParameters
object(T): The object or array containing stringified values to parse.parseNested(boolean, optional): Whether to recursively parse nested objects or arrays. Defaults totrue.
Return Value
ParsedObjectValues<T>: The parsed object with stringified values converted to their actual types.
Tips
-
Properties whose input type is
stringthe return type will be typed asstring | GenericObject:const result = parseObjectValues({ a: "hello", b: { x: 2 } }); // result: { a: "hello", b: { x: 2 } }result.awill be typed asstring | GenericObjectThis is because a string may also be a validJSONstring, which can also be parsed into an object (key-value pairs). -
You can pass generics to control the return type (3rd generic):
import { parseObjectValues } from 'toolbox-x'; type MyInputObject = { a: string; b: number; }; const object: MyInputObject = { a: `{ "x": "10" }`, b: 666 }; // But can be controlled with generics type MyReturnType = { a: { x: number; }; b: number; }; const resultTyped = parseObjectValues<MyInputObject, true, MyReturnType>(object);MyReturnTypemust have the top level keys ofobject(e.g.,MyInputObject) and the values can be anything.
Example Usage
Conversion Rules
"true"==>true"false"==>false"null"==>null"undefined"==>undefined- Numeric strings (e.g.
"42") ==>42 - JSON strings (e.g.
'{"a":1}') ==>{ a: 1 }
Aliases
The following aliases can be used for the parseObjectValues function:
parseStringifiedObjectValues
Last updated: Sat, Jul 18, 2026 11:12:22AM (UTC)
