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

@aphorica/json-processor

v0.1.2

Published

Assemble various json and other files to single json object - remove comment fields

Downloads

10

Readme

@aphorica/json-processor

github: https://aphorica.github.io Web: https://aphorica.com

Environment: Node.js

Motivation

A recent project I worked on implemented a sophisticated process that was configuration driven, the configuration being specified in JSON files – a good idea, but the configuration file was getting unwieldy. I needed a way to break the configuration into more manageable pieces.

Another driving factor was that this application could have several kinds of inputs and outputs, each having their own partial configuration sets. These needed to be combined according options chosen in the UI. By breaking the configuration into multiple files, I could also address this feature.

There are other solutions for providing segmented configuration files, but they implement YAL (yet another language), and provide kitchen sink functionality.

What I was looking for was simple:

  • Stay within the JSON format — this should be doable with canonical JSON syntax, without having to extend the language.

  • Providing a commenting form might be a nice add-on, since I have to process the entire file, anyway.

  • I don't need or want anything more than this.

  • I want it nice and tiny.

I couldn't really find anything else on npm or otherwise, so I wrote this.

Enjoy, if you find it useful.

Installing

npm install @aphorica/json-processor

or

yarn add @aphorica/json-processor

Overview

The utility reads JSON files and looks for directives in the keys it encounters during traversal. Possible keys are:

This allows two things:

  1. Assembly of a large configuration from many smaller, more easily managed and containable configuration files.

  2. Meaningful commenting in a JSON file (Crockford indicates commenting was explicitly left out of the JSON specification, but I think they're useful.) Without json-processor, you can certainly add a comment prefix key in your objects, if you want, but in your program you'll have to handle them, yourself.

    json-processor provides a canonical form and implementation, so you don't have to think about it.

Content

Here is an example JSON file with both a comment and file directives:

{
  "!!": "The summary sheet retabulates the 'Clockify' input into its final form.  Note the individual tab sheets are created in another process (driven by 'by_contractor.json')",

  "links-actor":"target",
  "sheetStyle": {
    "font": {
      "name":"Calibri",
      "size":"10"
    }
  },
	"groups": [
    {"file": "{output}level_0.json"},
    {"file": "{output}level_10.json"},
    {"file": "{output}level_18.json"},
    {"file": "{input}level_20.json"}
	],
  "lookups": {
    "contractors": {
      "file": "[contractors]"
    }
  }
}

Notes on the file content:

  • The object specifying a file will be replaced in its entirety by the content in the file.

  • An array of items can be specified. If an array item is an object specifying a file the entire item will be replaced with the content in the specified file.

  • the {...} and [...] delimit substitution keys. See the Substitution topic, below.

In this example, the comment is long, but if you view it in an editor with soft wraps set (vscode, in my case), it looks fine.

Alternatively, you can break up comments into sections, if you like, using normal JSON syntax:

{
  "!!": [
     "Here is point 1",
     "Here is point 2",
     {"furthermore": "ad nauseum}
    ]
  ,
  "actual-data": {
      ...
  }
}

Any children under the comment key will be removed (you don't have to provide the comment key in child objects.)

If a comment is an array item, the item will be removed from the array and the array rebuilt, sans comment item (order will be maintained)

Substitution

Substitutions happen in the value field only for objects containing "file" keys. Substitution paths are listed in the "options/paths" argument on invocation (see next).

The substitution delimiters are "{ }".

When a delimited substitution value is encountered in a value, it will be replaced with the corresponding path, and the base path prepended.

if the completed path is absolute (per path.absolute() test), the base path will not be prepended.

Invoking

  processed = processJSON([base-path], file, [options])

Args:

Example Invocation

Here is an example invocation from an app I'm working on:

  return processJSON(Settings.outputsConfigPath, 
                        'config.json', {  // 'options'
                        paths: {
                          output: Settings.outputsConfigPath,
                          input: Settings.inputsConfigPath,
                          contractors: Settings.contractorsFile
                        }})

This uses paths contained in a Settings object. For another example, see the test file.

Including Other File Types

You can include other file types by providing a list of objects, each specifying a file extension and a processor to return a js object. For instance, a text file with arbirtrarily formatted information could be processed by including it in a JSON file like so:

  {
    ...
    "other": {
      "file": "somefile.txt"
    }
    ...
  }

And providing the extension and processor in the options:

  function parseText(text) {
    let retObj = {}
    // build up the object by parsing the text
    return retObj 
  }

  processJSON(path, fn, {
    types: {
      ".txt": procText
    }
  })

You can process YAML by providing a YAML parser.

  import yaml from 'js-yaml'

  function procYaml(text) {
    yaml.loadSafe(text) // provide additional args here, if needed
  }

  processJSON(path, fn, {
    types: {
      ".yml": procYaml
    }
  })

See the test for working examples

Notes

  • All paths are expected to be trailed with a path separator.

  • On the name: I initially wanted to call it 'json-pp' for 'json pre-processor', however pre-processor isn't exactly right, since the files are read and parsed before any processing.

    So, I'm just calling it 'json-processor'