Toolbox-XToolbox-X

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): Result

Parameters

  • object (T): The object or array containing stringified values to parse.
  • parseNested (boolean, optional): Whether to recursively parse nested objects or arrays. Defaults to true.

Return Value

  • ParsedObjectValues<T>: The parsed object with stringified values converted to their actual types.

Tips

  • Properties whose input type is string the return type will be typed as string | GenericObject:

    const result = parseObjectValues({ a: "hello", b: { x: 2 } });
    // result: { a: "hello", b: { x: 2 } }

    result.a will be typed as string | GenericObject This is because a string may also be a valid JSON string, 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);

    MyReturnType must have the top level keys of object (e.g., MyInputObject) and the values can be anything.

Example Usage

playground.ts

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)

On this page