• Simple wrapper method to JSON.parse that catches any errors and optionally logs them to the console. This method is useful when you want to parse JSON but don't want to crash the application if the JSON is invalid.

    Type Parameters

    • T = any

    Parameters

    • jsonString: string

      The JSON string to parse

    • logErrors: boolean = false

      If true, parsing errors will be logged to console (default: false)

    Returns T

    The parsed object of type T (default: any), or null if parsing fails or input is empty

    Example

    // Basic usage without type
    const data = SafeJSONParse('{"name": "test"}');

    Example

    // With type parameter
    interface User { name: string; age: number; }
    const user = SafeJSONParse<User>('{"name": "John", "age": 30}', true);

    Example

    // Invalid JSON returns null
    const result = SafeJSONParse('invalid json', true); // logs error, returns null