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

json-stats-tool

v1.1.0

Published

gather statistics from JSON input streams

Downloads

2

Readme

node-json-stats: streaming stats from JSON files

This program is useful for getting simple statistics from JSON formatted files.

Install

npm install json-stats-tool

Sample Usage

Given this sample input file:

{"name": "john", "age": 10, "occupation": "student"}
{"name": "bob", "age": 10, "occupation": "student"}
{"name": "lucy", "age": 16, "occupation": "student"}
{"name": "linus", "age": 18, "occupation": "student"}
{"name": "charlie", "age": 18, "occupation": "student"}
{"name": "larry", "age": 50, "occupation": "doctor"}
{"name": "steve", "age": 50, "occupation": "doctor"}
{"name": "edna", "age": 45, "occupation": "programmer"}
{"name": "marcy", "age": 33, "occupation": "programmer"}
{"name": "darcy", "age": 30, "occupation": "programmer"}

We can get the average age of people by occupation:

$ ./json-stats-tool.js -o average -m age -d occupation < sample.json
OCCUPATION METRIC    AVERAGE
student    age         14
doctor     age         50
programmer age         36

Or the sum of their ages:

$ ./json-stats-tool.js -o sum -m age -d occupation < sample.json
OCCUPATION METRIC        SUM
student    age         72
doctor     age        100
programmer age        108

Or the count of people who are at the same age:

$ ./json-stats-tool.js -o count -m age < sample.json
          METRIC      COUNT
          10          2
          16          1
          18          2
          30          1
          33          1
          45          1
          50          2

Or do lots of stuff at once!

$ ./json-stats-tool.js -o sum,average,median -m age -d occupation < sample.json
OCCUPATION METRIC        SUM    AVERAGE     MEDIAN
student    age         72         14         16
doctor     age        100         50         50
programmer age        108         36         33

This works for nested objects too.

For example, say we're looking at a log file from a web server. Maybe we want to know which HTTP routes are sending back certain HTTP return codes.

If we have a JSON structure that looks something like this:

{ "route": <route_name>, "res": { "statusCode": <statusCode>, "headers": <headers> } }

We could easily find the count of status codes returned by each route:

$ grep '_audit' < muskie.log | ./json-stats-tool.js -m res.statusCode -d route -o count
ROUTE       METRIC      COUNT
headrootdir 200          3
getrootdir  200          2
unknown     404          7
headstorage 200          2
putobject   204          2
getstorage  200          2

Or if you wanted to see where the most requests are coming from you can pipe the output from this tool into other things:

$ grep 'audit' < webserver.log | ./json-stats-tool.js -m remoteAddress -o count -H | sort -n -k 2 | tail -n 5
          172.29.1.101       1644
          172.27.1.64        2201
          172.29.1.103       3309
          172.29.2.104       3765
          172.29.1.235      30724

json-stats also supports streaming operation where the results are calculated and printed every second. The examples above would also work with the input being tail -f instead of a redirected file.

License

MIT