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-combiner

v2.1.0

Published

Combines JSON file, using folder structure to determine JSON structure

Downloads

105

Readme

Build Status

json-combiner

json-combiner is a tool that ingests a folder of source files to produce a single JSON file with the same structure as the source folder. json-combiner will take the following types of files:

  • .json - Standard JS comments are stripped out of these files
  • .json5 - https://json5.org/
  • .js - Uses Node's require() to load the file. See below.
  • .ts - Uses ts-node and Node's require() to load the file. See below.

Installation

npm install json-combiner

Usage:

-s, --src  Source to folder of files to combine
-o, --out  Output JSON path (single file)
-m, --minify  If the output file should be minified. Default is false.

json-combiner -s ./configSrc/ -o ./lib/config.json

Examples

Assuming we have the following source JSON files:

src/foo.json:

{
    "title": "The Foo",
    "name":  "A wonderful component"
}

src/bar.json:

{
    "title": "The Bar",
    "name":  "An even more wonderful component"
}

Will generate the following JSON file as output:

{
    "foo": {
        "title": "The Foo",
        "name":  "A wonderful component"
    },
    "bar": {
        "title": "The Bar",
        "name":  "An even more wonderful component"
    }
}

Merging of Files and Folders

If a .json file and a folder share the same name, they will be merged into one object when the JSON is concatenated. Assuming we have the following source JSON files:

src/foo.json:

{
    "default": {
        "title": "The Foo",
        "name":  "A wonderful component"
    }
}

src/foo/bar.json:

{
    "title": "The Bar",
    "name":  "An even more wonderful component"
}

Will generate the following JSON file as output:

{
    "foo": {
        "default": {
            "title": "The Foo",
            "name":  "A wonderful component"
        },
        "bar": {
            "title": "The Bar",
            "name":  "An even more wonderful component"
        }
    }
}

Folder-as-Array Example

The contents of a folder can be grouped together as an array. The folder name must end in '[]'. For the files

  • src/foo[]/foo1.json:
  • src/foo[]/foo2.json:
  • src/foo[]/foo3.json:
{
    "foo": [
        {
            //contents of foo1.json...
        },
        {
            //contents of foo2.json...
        },
        {
            //contents of foo3.json...
        },
    ]
}

Note, that the .json files in an array folder do not retain their file names as keys, since they are now array index items.

Handling JavaScript files

A JavaScript file will be required (require()), and the module export (module.exports = ...) will be used as the object to be stringified. If the module export is a function, then that function will be called to obtain the result - if you need to do async stuff then return a promise from your function.

module.exports = {
    TWO_PI: Math.PI * 2,
    foo: "bar"
};
module.exports = () => {
    return new Promise((resolve) => {
        let rtn = [];
        for(let i = 100; i > 50; --i)
            rtn.push(i);
        //The return value here is the final result, which saves us from having to make our array
        //of integer values from 100 to 51 by hand.
        resolve(rtn);
    });
};

Handling TypeScript files

A TypeScript file will be required (require()) using ts-node, and the module export (export = ...) will be used as the object to be stringified. If the module export is a function, then that function will be called to obtain the result - if you need to do async stuff then return a promise from your function. NOTE: ts-node is a peer dependency, you must provide the version you wish to use.

export = {
    TWO_PI: Math.PI * 2,
    foo: "bar"
};

Code as Text

If for some reason you want to have JavaScript code inserted into your JSON as a string, use a file with a .text.js or .text.ts (must provide your version of TypeScript) extension. The JavaScript code will be added to the output as a string instead of running the code for a result.

Credits

This was originally implemented in https://github.com/SpringRoll/grunt-concat-json