Parse JSON into a typed value with a guard
Safely turn unknown JSON into a typed result by validating its shape instead of casting.
- TypeScript
- GreenSnippets
- MIT
- Executed with Node.js 22 (type stripping)
interface Product {
id: number;
name: string;
tags: string[];
}
function isProduct(value: unknown): value is Product {
if (typeof value !== "object" || value === null) return false;
const v = value as Record<string, unknown>;
return (
typeof v.id === "number" &&
typeof v.name === "string" &&
Array.isArray(v.tags) &&
v.tags.every((t) => typeof t === "string")
);
}
function parseJson<T>(text: string, guard: (value: unknown) => value is T): T | null {
try {
const data: unknown = JSON.parse(text);
return guard(data) ? data : null;
} catch {
return null;
}
}Example / expected output
const product = parseJson('{"id":1,"name":"Pen","tags":["office"]}', isProduct);
// Product | nullHow it works
JSON.parse returns any, which silently turns off type checking. Parse into unknown and run a type guard; only then does the compiler treat the value as your type. For larger schemas consider a validation library, but this pattern needs no dependencies.
Related snippets
- Python Tested
Safely read a deeply nested value
Walk a path through nested dicts and lists and return a default instead of raising KeyError.
- PHP Tested
Validate emails and URLs in PHP
Use filter_var to validate email addresses and http(s) URLs, with an allow-list for schemes.
- TypeScript Tested
Typed fetch helper that checks the response
A fetch wrapper that throws on HTTP errors and validates the JSON body with a type guard.
- Python Tested
Dataclass with validation
Validate and normalize fields in __post_init__ so invalid objects can never be created.

