如何在 Javascript 中更改 JSON 值模式

How to change JSON value schema in Javascript

本文关键字:JSON 模式 Javascript      更新时间:2023-09-26

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

默认情况下,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数据类型中。

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

例如:id & phone = numberinteger , email, name = string , dob= DATE , 地址 = TEXT 和国家 = 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中有一个有效的数据,你必须验证你的输入,例如使用Conform(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
}

我已经创建了一个如何做到这一点的小示例,您必须自己添加缺少的解析器。

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

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