Skip to content
Justice Tech School

JSON and Streams

JSON.parse and JSON.stringify convert data. Streams process large data in chunks instead of loading everything in memory. readline reads a stream line by line.

Example
const { Readable } = require("stream");
const readline = require("readline");
const data = { name: "Ada", skills: ["js", "node"] };
const text = JSON.stringify(data, null, 2);
console.log(JSON.parse(text).skills.length);
const rl = readline.createInterface({ input: Readable.from(["one\ntwo\nthree\n"]) });
const lines = [];
rl.on("line", (l) => lines.push(l.toUpperCase()));
rl.on("close", () => console.log(lines));

Result

2
[ 'ONE', 'TWO', 'THREE' ]

Exercise

Turn an object into text.

JSON.(obj)

Quiz Test

Why use streams?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.