Developer Documentation
TypeScript
Updated September 19, 2026

Validate JSON in TypeScript

Validate JSON syntax and handle parser errors without mutating the input in TypeScript.

Syntax validation answers one question: can the input be parsed as JSON? It is different from JSON Schema validation, which checks the shape and constraints of already valid JSON data.

Quick Start#

The following example uses the standard TypeScript API or a widely used package for this task.

Syntax validation#

TypeScript
1type ValidationResult = { valid: true } | { valid: false; error: string };23function validateJson(input: string): ValidationResult {4  try {5    JSON.parse(input);6    return { valid: true };7  } catch (error) {8    return {9      valid: false,10      error: error instanceof Error ? error.message : "Invalid JSON",11    };12  }13}

Common Operations#

This second pattern covers a common production operation or a safer way to handle real input.

Validate shape after parsing#

TypeScript
1function isUser(value: unknown): value is { name: string } {2  return typeof value === "object" && value !== null3    && "name" in value4    && typeof (value as { name?: unknown }).name === "string";5}67const parsed: unknown = JSON.parse(input);8if (!isUser(parsed)) throw new Error("JSON has the wrong shape");

Important Notes#

A TypeScript interface is erased at runtime; external JSON still needs runtime validation or a schema validator.

Best Practices#

  1. Treat external input as untrusted and handle parse/validation errors explicitly.
  2. Keep encoding, units, formats, and library-specific mapping rules explicit at system boundaries.
  3. Add test vectors that cover empty input, Unicode text, malformed input, and representative production data.

References#

Implement JSON Validator in other languages: