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
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Working with JSON & External API | TypeScript Mastery lesson | BraveProgrammer
  1. Courses
  2. /
  3. TypeScript Mastery

Lesson 23 of 56

·Typescript

Working with JSON & External APIs

20 minutes

Course progress41%

Parsing JSON

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

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

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.
Make it stickPractice what you've learned1 quiz · 3 practical tests · Optional activities from TypeScript MasteryBrowse activities
Previous lessonDeclaration Merging & Ambient DeclarationsContinue to lesson 24Async/Await with TypesBack to TypeScript Mastery curriculum