Created
March 18, 2024 11:58
-
-
Save bpsushi/201669faa4b08aaac2ed1d4c0079d588 to your computer and use it in GitHub Desktop.
Process gzip json files as streams
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Example JSON | |
| */ | |
| //{ | |
| // "books": [ | |
| // { "title": "The Witcher - The Last Wish" }, | |
| // { "title": "The Witcher - Sword of Destiny" }, | |
| // { "title": "The Witcher - Blood of Elves" }, | |
| // ] | |
| //} | |
| const https = require("node:https"); | |
| const Zlib = require("node:zlib"); | |
| const JSONStream = require("jsonstream"); | |
| https | |
| .get( | |
| "https://SOME_DOMAIN/my_file.json.gz", | |
| (res) => { | |
| res | |
| .pipe(Zlib.createGunzip()) | |
| .pipe(JSONStream.parse("books.*")) | |
| .on("data", (chunk) => { | |
| console.log("data chunk", chunk); | |
| }); | |
| res.on("end", () => { | |
| console.log("stream end"); | |
| }); | |
| } | |
| ) | |
| .on("error", (err) => console.log("Error:" + err.message, err)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment