Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

streaming-json-stringify

Package Overview
Dependencies
Maintainers
2
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

streaming-json-stringify

Streaming JSON.stringify()

  • 3.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
174K
increased by11.91%
Maintainers
2
Weekly downloads
 
Created

What is streaming-json-stringify?

The 'streaming-json-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 datasets that cannot be held entirely in memory.

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

Basic Usage

This code demonstrates the basic usage of the 'streaming-json-stringify' package. It reads an array of objects and writes them to a JSON file in a streaming manner, which is memory-efficient.

const stringify = require('streaming-json-stringify');
const fs = require('fs');

const data = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 3, name: 'Charlie' }
];

const readStream = require('stream').Readable({ objectMode: true });
readStream._read = () => {};
data.forEach(item => readStream.push(item));
readStream.push(null);

const writeStream = fs.createWriteStream('output.json');
readStream.pipe(stringify()).pipe(writeStream);

Custom Replacer Function

This example shows how to use a custom replacer function with 'streaming-json-stringify'. The replacer function omits the 'age' property from the JSON output.

const stringify = require('streaming-json-stringify');
const fs = require('fs');

const data = [
  { id: 1, name: 'Alice', age: 25 },
  { id: 2, name: 'Bob', age: 30 },
  { id: 3, name: 'Charlie', age: 35 }
];

const readStream = require('stream').Readable({ objectMode: true });
readStream._read = () => {};
data.forEach(item => readStream.push(item));
readStream.push(null);

const replacer = (key, value) => {
  if (key === 'age') return undefined;
  return value;
};

const writeStream = fs.createWriteStream('output.json');
readStream.pipe(stringify(replacer)).pipe(writeStream);

Custom Space for Pretty Printing

This code demonstrates how to use the 'streaming-json-stringify' package to pretty-print JSON output with a custom space parameter for indentation.

const stringify = require('streaming-json-stringify');
const fs = require('fs');

const data = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 3, name: 'Charlie' }
];

const readStream = require('stream').Readable({ objectMode: true });
readStream._read = () => {};
data.forEach(item => readStream.push(item));
readStream.push(null);

const writeStream = fs.createWriteStream('output.json');
readStream.pipe(stringify(null, 2)).pipe(writeStream);

Other packages similar to streaming-json-stringify

Keywords

FAQs

Package last updated on 29 Oct 2015

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