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

grunt-flatten-json

v0.0.5

Published

Flatten one or more JSON files into a single-level file

Readme

grunt-flatten-json Build Status

Flatten one or more JSON files into a single-level file

Getting Started

This plugin requires Grunt >=0.4.0

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-flatten-json --save-dev

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

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

This plugin was designed to work with Grunt 0.4.x. If you're still using grunt v0.3.x it's strongly recommended that you upgrade, but in case you can't please use v0.3.2.

Flatten JSON Task

Run this task with the grunt flatten_json command.

This task will combine one or more JSON files and flatten them into a single-level key.

Therefore, given the following JSON file:

{
  "key1": 
    {
      "subkey1": "sub key 1 value",
      "subkey2": "sub key 2 value"
    },
  "key2": 
    {
      "subkey1": "sub key 1 value",
      "subkey2": 
      {
        "subsubkey1": "sub sub key 1 value"
      }
    }
}   

The following flattened JSON file will be created:

{
  "key1.subkey1": "sub key 1 value",
  "key1.subkey2": "sub key 2 value"
  "key2.subkey1": "sub key 1 value",
  "key2.subkey2.subsubkey1": "sub sub key 1 value"
}   

This task was inspired by the need to make managing JSON-based translation files.

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

Options

encoding

Type: String
Default: grunt.file.defaultEncoding

The file encoding to read and write the JSON files with.

separator

Type: String
Default: .

The flattening key separator to use.

baseKey

Type: String
Default: null

A base key to use as a prefix to all flattened keys.

keyFilter

Type: String | Array | function(keyContext, key) Default: null

Either a specific key name, an array of key names or a function to exclude keys and their children from the flattened JSON results.

valueFilter

Type: function(value) Default: null

A function that filters all values processed during flattening and allows for re-writing.

Usage Examples

Generate multiple flattened JSON files

The following example demonstrates how to generate multiple flattened JSON files from sets of one or more JSON files.

flatten_json: {
  main: {
    files: [
      // flattens all JSON files directly under path that end in '_en_CA.json' 
      // to a file under dest called messages_en_CA.json
      {expand: true, src: ['path/*_en_CA.json'], dest: 'dest/messages_en_CA.json'},

      // flattens all JSON files under path that end in '_fr_CA.json' 
      // to a file under dest called messages_fr_CA.json
      {expand: true, src: ['path/**/*_fr_CA.json'], dest: 'dest/messages_fr_CA.json'}
    ],
  },
},

Specify a different key separator

The following example demonstrates how to generate a single flattened JSON file with a key separator of :.

flatten_json: {
  main: {
    options: {
      separator: ':'
    },
    src: ['path/*.json'], 
    dest: 'dest/messages.json'}
  },
},

The results of the might be something like the following:

{
  "key1:subkey1": "sub key 1 value",
  "key1:subkey2": "sub key 2 value"
}   

Specify a base key

The following example demonstrates how to generate a single flattened JSON file with a base key.

flatten_json: {
  main: {
    options: {
      baseKey: 'myBase'
    },
    src: ['path/*.json'], 
    dest: 'dest/messages.json'}
  },
},

The results of the might be something like the following:

{
  "myBase.key1.subkey1": "sub key 1 value",
  "myBase.key1.subkey2": "sub key 2 value"
}   

Exclude keys

Given the following JSON file:

{
  "key1":
    {
      "key1": "value 1",
      "key2": "value 2"
    },
  "key2":
    {
      "key1": "value 1",
      "key2": "value 2"
    },
  "key3":
    {
      "key1": "value 1",
      "key2": "value 2"
    }
}

The following example demonstrates excluding all keys of a specific string:

flatten_json: {
  main: {
    options: {
      keyFilter: 'key2'
    },
    src: ['path/*.json'],
    dest: 'dest/messages.json'}
  },
},

Would produce the following result:

{
  "key1.key1": "value 1",
  "key3.key1": "value 1",
}

The following example demonstrates excluding all keys of an array of keys:

flatten_json: {
  main: {
    options: {
      keyFilter: ['key2', 'key3']
    },
    src: ['path/*.json'],
    dest: 'dest/messages.json'}
  },
},

Would produce the following result:

{
  "key1.key1": "value 1"
}

The following example demonstrates using a function to exclude keys:

flatten_json: {
  main: {
    options: {
      keyFilter: function(keyContext, key) {
        return /key3$/.test(keyContext) && key == 'key1';
      }
    },
    src: ['path/*.json'],
    dest: 'dest/messages.json'}
  },
},

Would produce the following result:

{
  "key1.key1": "value 1",
  "key1.key2": "value 2",
  "key2.key1": "value 1",
  "key2.key2": "value 2",
  "key3.key2": "value 2"
}

Re-write values

The following example demonstrates using a function to re-write the values during flattening:

flatten_json: {
  main: {
    options: {
      valueFilter: function (value) {
        return value.replace(/\{([a-zA-Z0-9.]+)\}/gi,
          function (match, v) {
            return '{{' + v + '}}';
          });
      }
    },
    src: ['path/*.json'],
    dest: 'dest/messages.json'}
  },
},

Which would convert value like This is a {placeholder} value to This is a {{placeholder}} value.