如何在Javascript中更改JSON值模式

我从excelsheet获取数据,并使用Node.js中的xlsx-to-json将其转换为JSON格式

JSON数据的所有值默认显示为string格式,如:

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

当我将这个json插入到mongodb中时,所有的值都被存储在string数据types中。

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

例如:id&phone = numberinteger ,email,name = string ,dob = DATE ,address = TEXT和country = ENUM

最终的输出应该是这样的:

 var jsonObjResult = [ { id: 101, //integer email: 'user1@test.com', //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: 'user2@test.com', //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: 'user3@test.com', //string name: 'user3', // string dob: '1990-10-12', //date phone: '1234567890', //number country: ['England', 'Australia', 'France'], // enum address: 'Building 201-A, Abc, Xyz' // text } ]; 

任何帮助,将不胜感激。

如果你想在MongoDB中有一个有效的数据,你必须validation你的input,例如Conform(Revalidator分叉 – https://www.npmjs.com/package/conform )。 使用选项“castSource”将会转换源对象的值,然后将正确types的数据插入到数据库中。

 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 } 

我已经创build了一个小例子,你将不得不自己添加缺less的parsing器。

首先创build一个parsing器,这仅仅是一个为每个需要转换的对象键提供函数的对象。

 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. });