如何在 Javascript 中更改 JSON 值模式
How to change JSON value schema in Javascript
我正在从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 = number
或 integer
, 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.
});
相关文章:
- json模式v4如何根据枚举实现枚举
- 将json模式转换为角度树控制树模型
- JSON数据应该覆盖JSON模式吗?(通过jdorn/json编辑器使用模式和数据)
- 从JSON模式创建表单,如何添加关闭按钮
- 使用json模式的mocha全覆盖测试
- 从JSON模式自动生成JavaScript
- 如何从json模式创建表单
- json模式没有'不适用于空的响应体
- json模式获取js中给定属性的模式
- 如何对JSON文件运行javascript JSON模式
- 为什么NaN不是json模式基元类型
- json模式的构造函数
- 如何使用JSON模式验证表单
- 如果传递的是数组而不是对象,json模式将通过验证
- java类的JSON模式
- 将属性设置为“字符串”或在JSON模式中使用maxLength为空
- JSON模式+ JSON编辑器:具有可切换列的表
- 如何使用Apigee Edge策略进行JSON模式验证
- 我如何确保我的项目中的所有JSON模式都是有效的
- JSON模式解析器