jonschlinkert
jonschlinkert

Reputation: 11007

Using gruntjs, how would I get data from "nested" json files?

Let's say in my Gruntfile I have pkg: grunt.file.readJSON('package.json'), and inside the package.json is the following object:

{
  "file": "data.json"
}

How would I access the data from data.json? Which might look something like this:

{
  "name": "Jon Schlinkert",
  "company": "Sellside"
}

Upvotes: 0

Views: 329

Answers (2)

Sindre Sorhus
Sindre Sorhus

Reputation: 63478

Just load the first file, then use the result of that to load the second file and add it to the grunt config. Like this:

module.exports = function (grunt) {
    var pkg = grunt.file.readJSON('package.json');

    grunt.initConfig({
        pkg: pkg,
        data: grunt.file.readJSON(pkg.file),
        task: {
            target: {
                files: {
                    'dest': '<%- data.name %>'
                }
            }
        }
    });

    grunt.registerMultiTask('task', function() {});

    console.log('name', grunt.config('data.name'));
};

Upvotes: 1

asgoth
asgoth

Reputation: 35829

Maybe I don't understand the problem, but what about:

var pkg = grunt.file.readJSON('package.json');
var data = grunt.file.readJSON(pkg.file);

Upvotes: 0

Related Questions