javascript - Creating a folder structure from definitions on a json file - Stack Overflow

how can i create a folder structure using this json structure{"europe": {"poland":{

how can i create a folder structure using this json structure

{
  "europe": {
    "poland":{},
    "france":{},
    "spain":{},
    "greece":{},
    "UK":{},
    "germany":"txt"
  },
  "Asia": {
    "india":"xml"
  },
  "Africa":null
}

in such a way that

  1. properties with object values bee folders
  2. properties with string values are files and their values represent their extensions
  3. properties with null values are files with no extension
  4. nested objects bee nested folders

anyone know how to do this in nodejs?

how can i create a folder structure using this json structure

{
  "europe": {
    "poland":{},
    "france":{},
    "spain":{},
    "greece":{},
    "UK":{},
    "germany":"txt"
  },
  "Asia": {
    "india":"xml"
  },
  "Africa":null
}

in such a way that

  1. properties with object values bee folders
  2. properties with string values are files and their values represent their extensions
  3. properties with null values are files with no extension
  4. nested objects bee nested folders

anyone know how to do this in nodejs?

Share Improve this question asked Jan 31, 2015 at 21:52 shanksshanks 92213 silver badges24 bronze badges 2
  • So Africa is a file, not a folder ? – adeneo Commented Jan 31, 2015 at 21:54
  • Yes. Just a file without an extension. A folder is always represented with an object, which can be either empty or containing other nested objects – shanks Commented Jan 31, 2015 at 21:55
Add a ment  | 

2 Answers 2

Reset to default 3

You'll have to recursively iterate and create folders and files.

Not tested, but something like this should be close

var fs  = require('fs');
var obj = {
    "europe": {
        "poland": {},
        "france": {},
        "spain": {},
        "greece": {},
        "UK": {},
        "germany": "txt"
    },
    "Asia": {
        "india": "xml"
    },
    "Africa": null
};

(function create(folder, o) {
    for (var key in o) {
        if ( typeof o[key] === 'object' && o[key] !== null) {
            fs.mkdir(folder + key, function() {
                if (Object.keys(o[key]).length) {
                    create(folder + key + '/', o[key]);
                }
            });
        } else {
            fs.writeFile(folder + key + (o[key] === null ? '' : '.' + o[key]));
        }
    }
})('/', obj);

FIDDLE

Update

I was also looking for a solution to create files and folders based on a JSON and found this interesting node module "node-jsondir"

Here is an example of how it works:

var jsondir = require('jsondir');

jsondir.json2dir({
    "-path": 'path/to/directory',
    "myfile": {
        "-content": 'Hello world!'
    },
    "mydir": {
        "a": {
            "b": {
                "c": {
                    "-type": 'd'
                }
            }
        },
        "1": {
            "2": {
                "3": {}
            }
        }
    }
}, function(err) {
    if (err) throw err;
});

Tested and it works.

Node module is https://www.npmjs./package/jsondir

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745418823a4626874.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信