npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vstream-json-parser

v1.0.1

Published

vstream-based JSON parser stream

Downloads

849

Readme

vstream-based JSON parser

Synopsis

var JsonParserStream = require('vstream-json-parser');
var stream = new JsonParserStream();
stream.end('{ "hello": "world" }');
console.error(stream.read());

This prints:

{ hello: 'world' }

For a more complete example, see below.

Description

This module provides a Node object-mode Transform stream.

Inputs: chunks of UTF8 text, each representing a JSON object. The common use-case is to parse newline-separated JSON objects. You can do this by piping your input to an lstream and piping that to this stream. There's an example below.

Outputs: plain JavaScript objects, the result of parsing each input as JSON.

Error handling: This module follows the Joyent Best Practices for Error Handling in Node.js. There are two operational errors associated with this stream:

  • an input object could not be parsed as JSON
  • an input object was parsed and had the value null

These are not emitted as error events, since they would be fatal to the stream. Instead, these are emitted as a warning via the vstream interface. See the example below for details.

The only supported constructor argument is an optional object of stream options. The values in this object are passed to the Node Stream constructor, so this allows you to set things like highWaterMark. The objectMode property is always overridden to true.

Runtime notes: This class executes transformations synchronously, but never processes more than one input during a single tick in order to preserve liveness (i.e., to allow other events to be processed, even when a lot of inputs have been read).

The default highWaterMark is 0, meaning that the only data buffered are objects currently being processed. This class can be used to process large streams of data with a small amount of memory usage.

Objects with value null are ignored, because Node does not provide a way to emit them, nor do readers have a way of receiving them. (Emitting null would end the stream, and getting null from read() means end-of-stream.)

Full example: command-line program to extract fields from JSON objects

For a full example, see examples/extracttime.js. This example is a very simple tool that reads newline-separated JSON on stdin, extracts the field "time" from each one, and prints it out. If you were really doing this, you'd probably want to check out json. But this example includes error handling and is quite short.

As sample input, try passing it a bunyan log. Or try a file like this:

$ cat input 
{ "time": "now" }
{ "time": null }
{ "time
{ "time": "today" }
{ "notime": "tomorrow" }
{ "time": "yesterday" }

On that input, it emits this on stdout:

now
today
yesterday

and this on stderr:

warn: extractor input 2 from json parser input 2: value { time: null }: object's "time" is null
warn: json parser input 3: value '{ "time': Unexpected token t
warn: extractor input 4 from json parser input 5: value { notime: 'tomorrow' }: object has no "time"