|

Day 21 of 100: JSON — Parsing and Validating API Payloads

Day 21 of 100 — 100 Days of Playwright with TypeScript Challenge. Phase: JavaScript Fundamentals. JavaScript (Days 1-33) → TypeScript (Days 34-63) → Playwright (Days 64-96) → AI Framework Capstone (Days 97-100).

🎭 Want to master this with real projects? Join the Playwright Automation Mastery course at The Testing Academy.


JSON is the wire format of every API you test. Parse, stringify, validate — daily operations.

Contents

Parse and Stringify

const raw = '{"id": 1, "name": "Jane", "tags": ["qa", "admin"]}';

const user = JSON.parse(raw);     // string → object
user.tags[0];                      // 'qa'

const out = JSON.stringify(user);            // object → string
const pretty = JSON.stringify(user, null, 2); // indented — for logs/files

🚀 Level Up Your Playwright

From locators to CI pipelines — build a production-grade Playwright + TypeScript framework step by step.

Safe Parsing

function safeParse(text) {
  try {
    return { ok: true, data: JSON.parse(text) };
  } catch {
    return { ok: false, data: null };
  }
}

safeParse('not json');  // { ok: false, data: null }
// APIs sometimes return HTML error pages — never parse blindly

QA Pattern: Shape Validation

function validateUserShape(body) {
  const errors = [];
  if (typeof body.id !== 'number') errors.push('id must be number');
  if (typeof body.name !== 'string') errors.push('name must be string');
  if (!Array.isArray(body.tags)) errors.push('tags must be array');
  return errors;
}

validateUserShape({ id: '1', name: 'Jane', tags: [] });
// ['id must be number']

Tomorrow (Day 22): Callbacks — the pattern under everything async.

🎓 Master Playwright End to End

Join hundreds of SDETs building real automation frameworks. Lifetime access, hands-on projects, and a job-ready portfolio.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.