• Combines CleanJSON and SafeJSONParse to clean, extract, and parse JSON in one operation. This is a convenience function that first cleans the input string using CleanJSON to handle various formats (double-escaped, markdown blocks, etc.), then safely parses the result.

    Type Parameters

    • T = any

    Parameters

    • inputString: string

      The string to clean and parse, which may contain JSON in various formats

    • logErrors: boolean = false

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

    Returns T

    The parsed object of type T, or null if cleaning/parsing fails

    Example

    // Parse double-escaped JSON
    const result = CleanAndParseJSON<{name: string}>('{\\"name\\": \\"test\\"}', true);
    // Returns: {name: "test"}

    Example

    // Parse JSON from markdown const data = CleanAndParseJSON<{id: number}>('json\n{"id": 123}\n', false); // Returns: {id: 123}

    Example

    // Parse complex AI response with type safety
    interface AIResponse {
    status: string;
    data: any;
    }
    const response = CleanAndParseJSON<AIResponse>(aiOutput, true);
    // Returns typed object or null