Member Junction
    Preparing search index...
    • 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 | null

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

      // Basic usage without type
      const data = SafeJSONParse('{"name": "test"}');
      // With type parameter
      interface User { name: string; age: number; }
      const user = SafeJSONParse<User>('{"name": "John", "age": 30}', true);
      // Invalid JSON returns null
      const result = SafeJSONParse('invalid json', true); // logs error, returns null