Skip to main content

Function: safeJsonParseArray()

safeJsonParseArray<T>(json: string, elementValidator: (item: unknown) => item is T): SafeJsonResult<T[]>

Defined in: shared/utils/jsonSafety.ts:159

Parses a JSON array and validates each element.

Type Parametersโ€‹

Tโ€‹

T

Element type expected inside the array.

Parametersโ€‹

jsonโ€‹

string

Raw JSON string to parse.

elementValidatorโ€‹

(item: unknown) => item is T

Type guard applied to each array element.

Returnsโ€‹

SafeJsonResult<T[]>

Structured result containing a typed array or an error message.

Remarksโ€‹

Ensures the top-level value is an array before validating each element using the supplied guard. The first failing element aborts validation and surfaces an informative error message.

Exampleโ€‹

import { logger } from "@app/services/logger";

const result = safeJsonParseArray(
jsonString,
(item): item is User => typeof item === "object" && item !== null
);

if (result.success) {
logger.info("Parsed users", { count: result.data.length });
}