Socket
Socket
Sign inDemoInstall

json-stream-stringify

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-stream-stringify

JSON.Stringify as a readable stream


Version published
Maintainers
1
Created

What is json-stream-stringify?

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.

What are json-stream-stringify's main functionalities?

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'));

Other packages similar to json-stream-stringify

FAQs

Package last updated on 30 Mar 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc