I have a huge collection of JSON files, extracted from a public API. I'm trying to get the schema of the object the files are describing (key/type of value) thanks to Node.js, the same way schemas are described in Mongoose, e.g. :
schema = {
key1: 'Number',
key2: 'Boolean',
key3: {
key31: 'String',
key32: 'Boolean'
},
key4: [{
key41: 'String',
key42: 'Number'
}]
};
In order to do this, I wrote a script which take the first file, get the keys and type of values (recursively, as the object has sub-objects) ; then compare the second file to the first one and add some data if the first file was missing some keys, and so on... Maybe not the best solution to achieve what I want to do, but it (almost) does the work.
Unfortunately, after running the script, all the types of array of sub-objects (key41
and key42
in example above) are shown as string
, although I have some boolean
,number
...
Here's what I did from now on :
var glob = require('glob'),
_ = require('lodash');
function getSchema(from, to) {
for (var kay in from) {
if (from.hasOwnProperty(key)) {
if (_.isObject(from[key])) {
//_.isObject() returns true for arrays
if (_.isArray(from[key])) {
if (!to.hasOwnProperty(key)) {
to[key] = [{}];
}
if (from[key].length) {
var tmp = {};
for (var i = 0; i < from[key].length; i++) {
getSchema(from[key][i], tmp);
}
getSchema(tmp, to[key][0]);
}
} else {
if (!to.hasOwnProperty(key)) {
to[key] = {};
}
getSchema(from[key], to[key]);
}
} else {
to[key] = typeof from[key];
}
}
}
};
glob('./data/**/*.json', {}, function(err, files) {
if (err) console.log(err);
var schema = {};
_.forEach(files, function(file) {
var data = require(file);
getSchema(data, schema);
});
//schema should be an object, mirroring the schema of the JSON files
});
Can someone help me fixing this ?
Thanks in advance.