@adviser/cement - v0.0.0
    Preparing search index...

    Function isJSON

    • Checks if a string contains valid JSON and optionally parses it.

      Uses a regex pre-check to quickly eliminate non-JSON strings before attempting to parse. Returns both a boolean flag and the parsed result if successful.

      Type Parameters

      • T = unknown

        The expected type of the parsed JSON (default: unknown)

      Parameters

      • str: string

        The string to check and parse

      Returns IsJSONResult<T>

      Object with isJSON boolean and optional parsed value

      const result = isJSON<{ name: string }>('{"name": "Alice"}');
      if (result.isJSON) {
      console.log(result.parsed.name); // "Alice"
      }

      const invalid = isJSON('not json');
      console.log(invalid.isJSON); // false