bhylu.com

Advertisements

JSON Parse

Parse JSON inside a suitable error boundary when the input may be invalid. Parsing alone does not validate the record shape.

Where to run this

This example uses the JavaScript console playground. Select Run to execute it; the example does not run on this lesson page.

A practical example

JavaScript

const session = JSON.parse('{"places":12}');
if (!Number.isInteger(session.places)) throw new TypeError("Invalid place count");
console.log(session.places);

What happens

The parsed field passes the integer check.