Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
ldjson-stream
Advanced tools
The ldjson-stream npm package is designed to handle line-delimited JSON (LDJSON) streams. It provides utilities for parsing and stringifying LDJSON data, which is useful for processing large datasets in a memory-efficient manner.
Parsing LDJSON
This feature allows you to parse a stream of line-delimited JSON data. The code sample reads from a file named 'data.ldjson' and parses each line as a JSON object, logging each object to the console.
const ldjson = require('ldjson-stream');
const fs = require('fs');
fs.createReadStream('data.ldjson')
.pipe(ldjson.parse())
.on('data', obj => {
console.log(obj);
});
Stringifying LDJSON
This feature allows you to stringify a stream of JSON objects into line-delimited JSON format. The code sample takes an array of objects and writes them to a file named 'output.ldjson' in LDJSON format.
const ldjson = require('ldjson-stream');
const fs = require('fs');
const data = [
{ name: 'Alice', age: 30 },
{ name: 'Bob', age: 25 }
];
const writeStream = fs.createWriteStream('output.ldjson');
const stringifyStream = ldjson.serialize();
stringifyStream.pipe(writeStream);
data.forEach(item => stringifyStream.write(item));
stringifyStream.end();
JSONStream is a popular package for working with streaming JSON data. It provides similar functionality to ldjson-stream, including parsing and stringifying JSON data. However, JSONStream is more general-purpose and can handle various JSON formats, not just line-delimited JSON.
ndjson is another package specifically designed for handling newline-delimited JSON streams. It offers similar features to ldjson-stream, such as parsing and stringifying NDJSON data. The main difference is in the API design and implementation details.
event-stream is a toolkit for working with streams in Node.js. It includes utilities for parsing and stringifying JSON data, among other stream-related functionalities. While it is not focused solely on LDJSON, it can be used to achieve similar results.
var ldj = require('ldjson-stream')
returns a transform stream that accepts newline delimited json and emits objects
example newline delimited json:
data.txt
:
{"foo": "bar"}
{"hello": "world"}
If you want to discard non-valid JSON messages, you can call ldj.parse({strict: false})
usage:
fs.createReadStream('data.txt')
.pipe(ldj.parse())
.on('data', function(obj) {
// obj is a javascript object
})
returns a transform stream that accepts json objects and emits newline delimited json
example usage:
var serialize = ldj.serialize()
serialize.on('data', function(line) {
// line is a line of stringified JSON with a newline delimiter at the end
})
serialize.write({"foo": "bar"})
serialize.end()
BSD
FAQs
streaming line delimited json parser + serializer
We found that ldjson-stream demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.