Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. TypeScript Mastery

Lesson 23 of 56 · Typescript

Working with JSON & External APIs

Duration: 20 minutes

Parsing JSON

interface User { id: number; name: string; }

const raw = `[{"id":1,"name":"Alice"}]`;
const users: User[] = JSON.parse(raw);

Fetch API (browser & node ≥18)

async function getUser(id: number): Promise<User> {
  const resp = await fetch(`https://jsonplaceholder.typicode.com/users/${id}`);
  if (!resp.ok) throw new Error('Network error');
  const data = await resp.json();
  return data as User;
}

Runtime Validation with zod

npm i zod
import { z } from "zod";
const UserSchema = z.object({ id: z.number(), name: z.string() });
const parsed = UserSchema.parse(await resp.json());

Info

Never trust external JSON – always validate before using.

Previous: Declaration Merging & Ambient DeclarationsNext: Async/Await with Types