简体   繁体   中英

Compiling multiple json files into one with grunt-json-bake

I have a folder with about 400 json files that a former teammate pulled from an api with curl. I need to compile all those json files into one big json file. I am using the grunt-json-bake grunt plugin to do it. Heres my Gruntfile

/* global grunt */

module.exports = function (grunt) {

grunt.initConfig({
    json_bake: {
        "en": {
            options: {},
            files: {
                "dist/final.json": ["jsonFiles/**.json"]
            }
        }
    }
})

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

grunt.registerTask("default", ["json_bake"]);

}

Everytime I run this it only pulls the last json file into the dist/final.json, like it has not even looped through all the json files in the jsonFiles/ directory.

What json_bake does is parse a starter file listing some "include" directives and applying them, it does not concatenate files bluntly. They have a thorough example at https://github.com/MathiasPaumgarten/grunt-json-bake#recursive-bake-including-files-and-folders

So if you want to have the value of each file referenced as an entry in a file array property of your final.json , you need to first create a base.json in your root directory with:

{
  "files": "{{jsonFiles}}"
}

Then in your gruntfile you reference this base file:

json_bake: {
    "en": {
        files: {
            "dist/final.json": ["base.json"]
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM