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.
json-stream-stringify
Advanced tools
The json-stream-stringify npm package is designed to convert JavaScript objects into JSON strings in a memory-efficient manner by streaming the output. This is particularly useful for handling large objects that might otherwise consume too much memory if converted to JSON all at once.
Basic Usage
This feature demonstrates the basic usage of the json-stream-stringify package. It shows how to create a stream from a large JavaScript object and pipe it to a file, thereby converting the object to a JSON string in a memory-efficient way.
const JSONStreamStringify = require('json-stream-stringify');
const fs = require('fs');
const largeObject = { /* large object data */ };
const jsonStringifyStream = new JSONStreamStringify(largeObject);
jsonStringifyStream.pipe(fs.createWriteStream('output.json'));
Custom Replacer Function
This feature demonstrates how to use a custom replacer function with json-stream-stringify. The replacer function can be used to filter or modify the values being stringified. In this example, all string properties are excluded from the output.
const JSONStreamStringify = require('json-stream-stringify');
const fs = require('fs');
const largeObject = { /* large object data */ };
const replacer = (key, value) => {
if (typeof value === 'string') {
return undefined; // Exclude all string properties
}
return value;
};
const jsonStringifyStream = new JSONStreamStringify(largeObject, replacer);
jsonStringifyStream.pipe(fs.createWriteStream('output.json'));
Custom Space Argument
This feature demonstrates how to use the space argument to format the JSON output with indentation. In this example, the JSON output will be pretty-printed with an indentation of 2 spaces.
const JSONStreamStringify = require('json-stream-stringify');
const fs = require('fs');
const largeObject = { /* large object data */ };
const jsonStringifyStream = new JSONStreamStringify(largeObject, null, 2);
jsonStringifyStream.pipe(fs.createWriteStream('output.json'));
JSONStream is a package that provides streaming JSON parsing and stringifying. It is similar to json-stream-stringify in that it allows for memory-efficient handling of large JSON objects. However, JSONStream offers more flexibility with its ability to parse JSON streams in addition to stringifying them.
stream-json is another package that provides tools for working with JSON in a streaming fashion. It includes parsers, stringifiers, and utilities for processing JSON data in a memory-efficient way. Compared to json-stream-stringify, stream-json offers a more modular approach with separate components for different tasks.
oboe is a library for working with JSON in a streaming manner. It allows for both parsing and stringifying JSON data. Oboe is particularly useful for handling large JSON responses from HTTP requests. Compared to json-stream-stringify, oboe provides more features for working with JSON data in real-time.
JSON Stringify as a Readable Stream with rescursive resolving of any readable streams and Promises.
new
operator* Off by default since v2
** With polyfills
npm install --save json-stream-stringify
# Optional if you need polyfills
# Make sure to include these if you target NodeJS <=v6 or browsers
npm install --save @babel/polyfill @babel/runtime
Using Node v8 or later with ESM / Webpack / Browserify / Rollup
import { JsonStreamStringify } from 'json-stream-stringify';
install @babel/runtime-corejs3 and corejs@3
import { JsonStreamStringify } from 'json-stream-stringify/polyfill';
import { JsonStreamStringify } from 'json-stream-stringify/module.polyfill'; // force ESM
const { JsonStreamStringify } = require('json-stream-stringify'); // let module resolution decide UMD or CJS
const { JsonStreamStringify } = require('json-stream-stringify/umd'); // force UMD
const { JsonStreamStringify } = require('json-stream-stringify/cjs'); // force CJS
const { JsonStreamStringify } = require('json-stream-stringify/polyfill');
const { JsonStreamStringify } = require('json-stream-stringify/umd/polyfill');
const { JsonStreamStringify } = require('json-stream-stringify/cjs/polyfill');
Note: This library is primarily written for LTS versions of NodeJS. Other environments are not tested.
Note on non-NodeJS usage: This module depends on node streams library. Any Streams3 compatible implementation should work - as long as it exports a Readable
class, with instances that looks like readable streams.
Note on Polyfills: I have taken measures to minify global pollution of polyfills but this library does not load polyfills by default because the polyfills modify native object prototypes and it goes against the W3C recommendations.
new JsonStreamStringify(value[, replacer[, spaces[, cycle]]])
Streaming conversion of value
to JSON string.
value
Any
Data to convert to JSON.
replacer
Optional Function(key, value)
or Array
As a function the returned value replaces the value associated with the key. Details
As an array all other keys are filtered. Details
spaces
Optional String
or Number
A String or Number object that's used to insert white space into the output JSON string for readability purposes. If this is a Number, it indicates the number of space characters to use as white space. If this is a String, the string is used as white space. If this parameter is not recognized as a finite number or valid string, no white space is used.
cycle
Optional Boolean
true
enables cycling of cyclical structures and dags.
To restore cyclical structures; use Crockfords Retrocycle method on the parsed object (not included in this module).
JsonStreamStringify
object that exposes a Streams3 interface.Get current path begin serialized.
Array[String, Number]
.join('.')
..on('error', ...)
, for figuring out what path may have caused the error.const { JsonStreamStringify } = require('json-stream-stringify');
const jsonStream = new JsonStreamStringify({
// Promises and Streams may resolve more promises and/or streams which will be consumed and processed into json output
aPromise: Promise.resolve(Promise.resolve("text")),
aStream: ReadableObjectStream({a:1}, 'str'),
arr: [1, 2, Promise.resolve(3), Promise.resolve([4, 5]), ReadableStream('a', 'b', 'c')],
date: new Date(2016, 0, 2)
});
jsonStream.once('error', () => console.log('Error at path', jsonStream.stack.join('.')));
jsonStream.pipe(process.stdout);
Output (each line represents a write from jsonStreamStringify)
{
"aPromise":
"text"
"aStream":
[
{
"a":
1
}
,
"str"
]
"arr":
[
1
,
2
,
3
,
[
4
,
5
]
,
"
a
b
c
"
],
"date":
"2016-01-01T23:00:00.000Z"
}
app.get('/api/users', (req, res, next) => {
res.type('json'); // Required for proper handling by test frameworks and some clients
new JsonStreamStringify(Users.find().stream()).pipe(res);
});
install @types/readable-stream
or @types/node
or create your own stream.d.ts
that exports a Readable
class.
Copyright (c) 2016 Faleij faleij@gmail.com
FAQs
JSON.Stringify as a readable stream
The npm package json-stream-stringify receives a total of 274,292 weekly downloads. As such, json-stream-stringify popularity was classified as popular.
We found that json-stream-stringify demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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.