2016-03-08 49 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 
}