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

json-to-code

v0.2.0

Published

JSON-to-Code Encoding: Compress JSON Data into Actual Code

Readme

json-to-code

JSON-to-Code Compressor: Compress JSON Data into Valid JS Code that Generates the Data

install

npm install json-to-code

basic usage

const { encode } = require("json-to-code");

const json = {
  "agency": "GSA",
  "measurementType": {
    "method": "modules"
  },
  "version": "2.0.0",
  "releases": [
    {
      "name": "usasearch",
      "description": "System now maintained in open repo https://github.com/GSA/search-gov.",
      "permissions": {
        "licenses": null,
        "usageType": "governmentWideReuse"
      },
      "tags": [
        "GSA"
      ],
      "repositoryURL": "https://github.com/GSA/usasearch",
      "homepageURL": "https://search.gov",
      "contact": {
        "email": "[email protected]"
      },
      "laborHours": 0,
      "vcs": "git",
      "organization": "GSA"
    }
  ]
};

const { code } = encode({
  data: json,

  // optional code to set the result to
  prefix: "module.exports = " // could be window.data = 
})

code will be something like

/* pass 1 */
const A='[email protected]',B='repositoryURL',C='organization',D='permissions',E='description',F='http://choosealicense.com/licenses/other/',...
module.exports = {'agency':P,'measurementType':{'method':'modules'},'version':'2.0.0','releases':[{[N]:'usasearch',[E]:HE+' now maintained in open repo https://github.com/GSA/search-gov.',[D]:{[I]:null,[H]:L},[Q]:[P],[B]:'https://github.com/GSA/usasearch',[X]:Au,[K]:{[O]:A},[G]:0,[S]:T,[C]:P},{[N]:'cron_scripts',[E]:'The ~search/scripts directory on the CRON machine, containing all'+El+' for the'+" 'search'"+' user',[D]:{[I]:null,[H]:L},[Q]:[P],[B]:'https://github.com/GSA/cron_scripts',[K]:{[O]:A},[G]:0,[S]:T,[C]:P},...

advanced usage

limiting max passes

You can specify how many times json-to-code should loop over the data looking for saving opportunities. If speed is more important than compression quality, just set max_passes to 1.

const { code } = encode({
  data: json,
  max_passes: 1
});

editable

By default, encode assumes that you only want to read your data, so that it can increase compression by reusing common arrays. However, if you will want to edit your data, set read_only to false.

const { code } = encode({
  data: json,
  read_only: false
});

Python

If you want to generate Python code instead of JavaScript, set language to "Python".

const { code } = encode({
  data: json,
  language: "Python"
});

Let's say you save it to a file named "thing.py". You'll then import your data in Python like so:

from thing import data

Changing Prefix

If you prefer to change what variable your data is set to, you can edit the prefix.

const { code } = encode({
  data: json,
  prefix: "window.data"
});
require("fs").writeFileSync("data.js", code);

You can then load the data in an HTML file like so:

<script src="data.js"></script>

And access it inside a script

<script>
console.log(window.data);
// console.log(data) works too if you are in the browser
</script>