2016-03-08 50 views
0

我从excelsheet提取数据,并将其转换成Node.js的使用xlsx-to-jsonJSON格式如何改变JSON值模式在Javascript

所有JSON数据的值是默认显示像string格式:

var jsonObj = [ 
{ 
id: '101', // string 
email: '[email protected]', //string 
name: 'user1', 
dob: '1990-10-10', 
phone: '1234567890', //string 
country: 'England', 
address: 'Building 201-A, Abc, Xyz' 
}, 
{ 
id: '102', 
email: '[email protected]', 
name: 'user2', 
dob: '1990-10-11', 
phone: '1234567890', 
country: 'Australia', 
address: 'Building 201-A, Abc, Xyz' 
}, 
{ 
id: '103', 
email: '[email protected]', 
name: 'user3', 
dob: '1990-10-12', 
phone: '1234567890', 
country: 'France', 
address: 'Building 201-A, Abc, Xyz' 
} 
]; 

当我将这个json到MongoDB中所有的值都获得存储在string数据类型。

我想要做的是验证所有这个模式,并在将其插入到mongodb之前更改它的数据类型。

例:ID &手机= numberinteger,电子邮件,名称= string,DOB = DATE,地址= TEXT和国家= ENUM

最终输出应该是这样的:

var jsonObjResult = [ 
{ 
id: 101, //integer 
email: '[email protected]', //string 
name: 'user1', //string 
dob: '1990-10-10', //Date 
phone: '1234567890', //number 
country: ['England', 'Australia', 'France'], // enum 
address: 'Building 201-A, Abc, Xyz' // text 
}, 
{ 
id: '102', // integer 
email: '[email protected]', //string 
name: 'user2', // string 
dob: '1990-10-11', //date 
phone: '1234567890', // number 
country: ['England', 'Australia', 'France'], // enum 
address: 'Building 201-A, Abc, Xyz' // text 
}, 
{ 
id: '103', //integer 
email: '[email protected]', //string 
name: 'user3', // string 
dob: '1990-10-12', //date 
phone: '1234567890', //number 
country: ['England', 'Australia', 'France'], // enum 
address: 'Building 201-A, Abc, Xyz' // text 
} 
]; 

任何帮助将不胜感激。

+1

您可以简单地使用正则表达式! –

回答

0

我已经创建了一个如何做的例子,你将不得不自己添加缺少的解析器。

首先创建一个解析器,这只是一个对象,为每个需要转换的对象键提供一个函数。

var parsers = { 
    id: parseInt, 
    dob: function(str) { 
     return new Date(str); 
    }, 
    phone: parseInt 
}; 

// This will parse the object and apply the transform parsers from above, calls the callback when finished 
var parseObject = function(obj, callback) { 
    var counter = 0; 
    Object.keys(obj).forEach(function(key, index, array) { 
     if (parsers.hasOwnProperty(key) /* && typeof parsers[key] === 'function' */) { // typeof check is not really needed, when he trust that we only define functions in our parser above 
      obj[key] = parsers[key](obj[key]); 
     } 
     if (++counter === array.length) { 
      callback && callback(); // call the callback when all parsers have run 
     } 
    }); 
}; 

var parseJson = function(json, callback) { 
    var counter = 0; 
    for (var i = 0; i < json.length; i++) { 
     parseObject(json[i], function() { // parses all the objects 
      if (++counter === json.length) { 
       callback && callback(); // call the callback when all objects have been parsed 
      } 
     }); 
    } 
}; 

parseJson(jsonObj, function() { 
    console.log(jsonObj); // outputs the parsed object when everything is done. 
}); 
1

如果你想在MongoDB中一个有效的数据,您必须验证您例如输入与顺应(Revalidator的叉 - https://www.npmjs.com/package/conform)。使用选项'castSource'将会转换源对象的值,然后您将数据以正确的类型插入到数据库中。

var Conform = require('conform'); 

var myData = { 
    intField: '123' 
}; 

// after validate intField will be casted to integer 
var validateResult = Conform.validate(
    myData, 
    { 
     properties: { 
      intField: { 
       type: 'integer' 
      } 
     } 
    }, 
    { 
     cast: true, 
     castSource: true 
    }); 

if (validateResult.valid) { 
    // insert myData to db 
}