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

grunt-concat-json

v0.0.10

Published

Grunt Task for Merging Multiple JSON Files

Downloads

61

Readme

grunt-concat-json Build Status Dependency Status npm version

Grunt Task for Merging Multiple JSON Files

Getting Started

This plugin requires Grunt ~0.4.5

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-concat-json --save

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-concat-json');

Task Properties

src

Type: String|Array

The path to the source JSON files or collection of individual files. For instance: "src/**/*.{json,js}", which will concatenate all JSON and JS files in the src folder.

dest

Type: String

The path to the output concatenated JSON file.

cwd

Type: String Default: null

The root folder to source files from. This will exclude this folder and it's parents from nested layer representation in the output JSON file. If cwd is set, then the root folder does not need to be specified as part of the src.

Task Options

options.folderArrayMarker

Type: String Default: []

The token to use as suffix to a folder name to signify the contents should be rendered as an Array of items and not a Object.

options.replacer

Type: function Default: null

The replacer argument for JSON.stringify() (second argument).

options.space

Type: String Default: ""

The space argument for JSON.stringify() (third argument).

Merge JSON Task

Run this task with the grunt concat-json command.

Task targets, files and options may be specified according to the Grunt Configuring tasks guide.

Usage Example

Assuming we have the following types of source JSON files:

src/foo/foo-en.json:

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

src/bar/bar-en.json:

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

Will generate the following destination JSON file:

{
    "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 destination JSON file:

{
    "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 must end in a unique symbol, the default is '[]'; 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...
        },
    ]
}

Handling JavaScript files

The javascript file can take two forms - either an object literal, or the contents of a function where your return value becomes the JSON object for the file.

{
    //if the first character is the first character of an object literal, then it is evaluated that
    //way. This means that if your JSON as JS is set up that way, you can't have whitespace or
    //a comment as the first text
    TWO_PI: Math.PI * 2,
    foo: "bar"
}
//other javacript is wrapped within a function, allowing you to create your object however you like
var rtn;
for(var 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 form 100 to 51 by hand.
return rtn;

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

Single file per target variant

grunt.initConfig({
    "concat-json": {
        "en": {
            src: [ "src/**/*-en.json" ],
            dest: "www/en.json"
        },
        "de": {
            src: [ "src/**/*-de.json" ],
            dest: "www/de.json"
        }
    }
});

Multiple files per target variant

grunt.initConfig({
    "concat-json": {
        "i18n": {
            files: {
                "www/en.json": [ "src/**/*-en.json" ],
                "www/de.json": [ "src/**/*-de.json" ]
            }
        }
    }
});