Sometimes, we want to parse large JSON file in Node.js.
In this article, we’ll look at how to parse large JSON file in Node.js.
How to parse large JSON file in Node.js?
To parse large JSON file in Node.js, we call fs.createReadStream and use the JSONStream library.
To install it, we run
npm i JSONStream
Then, we write
const fs = require("fs");
const JSONStream = require("JSONStream");
const getStream = () => {
const jsonData = "myData.json";
const stream = fs.createReadStream(jsonData, { encoding: "utf8" });
const parser = JSONStream.parse("*");
return stream.pipe(parser);
};
getStream()
.pipe(transform)
.on("error", (err) => {
// ...
});
to call fs.createReadStream to read the file at path jsonData.
And then we call JSONStream.parse to create a parser object.
Next, we call stream.pipe with parser to parse the JSON that’s read from the stream.
Then we call getStream with pipe to transform the result with the transform function.
And we watch for errors with on.
Conclusion
To parse large JSON file in Node.js, we call fs.createReadStream and use the JSONStream library.