Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
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"}
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
The npm package ldjson-stream receives a total of 247,012 weekly downloads. As such, ldjson-stream popularity was classified as popular.
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.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.