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 🙏

© 2026 – Pkg Stats / Ryan Hefner

gen-json-parser

v1.1.5

Published

Generate JSON parsers for custom data structures.

Downloads

79

Readme

gen-json-parser

Generate JSON parsers for custom data structures.

Motivation

This project came about after realizing that JavaScript objects are not Maps. With a Map data-structure one should expect that for all keys k, adding a new entry at k cannot affect the existence of other entries. JavaScript objects do not have this property due to special keys such as "__proto__".

If you wish to treat JSON as a representation of data (for example, that a user may input) then you will want to avoid JSON.parse. What you want is to parse the JSON into data-structures that are appropriate to your solution.

Parser Results

Parse results are objects with a boolean status property. If parsing succeeds, the status is true and there is a value property associated with the parsed value. Otherwise the status is false with some information about the parse error.

Premade parsers

Setup:

var parse = require('gen-json-parser').parse;
var demo = '{ "foo": "bar", "key": [ 2, true, [ null ] ] }';

Identity

The identity parser translates JSON values to their JavaScript equivalents, except JSON objects are parsed into JavaScript arrays of pairs.

Example:

parse.Identity(demo);
> {
>   "status": true,
>   "value": [
>     [ "foo", "bar" ],
>     [ "key", [
>       2,
>       true,
>       [ null ]
>     ] ]
>   ]
> }

JValues

A JValue is a JavaScript object with two properties: tag and term.

tag is associated with a string value indicating a 'type', such as "boolean", "null", or "object".

term is associated with a value that has the type indicated by the tag string.

The JValues parser produces a JavaScript JValue. For example, the JSON "true" would map to the JavaScript value { "tag": "boolean", "term": true }.

Example:

parse.JValues(demo);
> {
>   "status": true,
>   "value": {
>     "tag": "object",
>     "term": [
>       [ "foo", { "tag": "string", "term": "bar" } ],
>       [ "key", {
>         "tag": "array",
>         "term": [
>           { "tag": "number", "term": 2 },
>           { "tag": "boolean", "term": true },
>           { "tag": "array", "term": [
>             { "tag": "null", "term": null }
>           ] }
>         ]
>       } ]
>     ]
>   }
> }

Custom parsers

To write a custom JSON parser, you must provide details of how to interpret each of the different JSON value types: null, boolean, string, number, array, object. If you do not provide an interpreter for a given type, then it will be a parse error for values of that type to appear in the JSON.

For example, to replicate the behaviour of JSON.parse, we could generate the following parser:

var generate = require('gen-json-parser').generate;
var id = function (v) { return v; };

var parseJSON = generate({
  null: id,
  boolean: id,
  string: id,
  number: id,
  array: id,
  object: function (pairs) {
    return pairs.reduce(function (acc, pair) {
      acc[pair[0]] = pair[1];
      return acc;
    }, {});
  }
});

Note that JSON.parse throws if there is a parse error, whereas this one will return an object with a status property associated with false.