Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以灵活的方式标准化JSON对象中的数据的最佳实践_Json_Node.js - Fatal编程技术网

以灵活的方式标准化JSON对象中的数据的最佳实践

以灵活的方式标准化JSON对象中的数据的最佳实践,json,node.js,Json,Node.js,我需要使用NodeJS将各种结构化格式的信息转换为其他格式(例如html或文本)。因此,我采用了基于JSON模式将源格式转换为JSON的方法。我还可以基于Pug模板将生成的JSON转换为文本 我现在寻找的是一种灵活的方法来标准化数据,简化JSON中的结构,从而减少日期和时间格式的变化 这种物体的一个例子是: { header: { sender: { // more properties id: '12345'; } r

我需要使用NodeJS将各种结构化格式的信息转换为其他格式(例如html或文本)。因此,我采用了基于JSON模式将源格式转换为JSON的方法。我还可以基于Pug模板将生成的JSON转换为文本

我现在寻找的是一种灵活的方法来标准化数据,简化JSON中的结构,从而减少日期和时间格式的变化

这种物体的一个例子是:

{
  header: {
      sender: {
         // more properties
         id: '12345';
      }
      receiver: {
         // more properties
         id: '987654';
      }
      date: {
         date: '170910',
         time: '0922'
      }
      // more properties
  }
  // more properties
  someMore: {
      birthdate: {
          year: 2016,
          month: 5,
          day: 11
      }
      otherProperty: {
          // more properties
          date: '20170205'
      } 
  }      
}
我想把这个转换成

{
  header: {
      senderId: '12345',
      receiverId: '987654'
      date: '20170910T0922'
  }
  // more properties
  someMore: {
      birthdate: '20160511',
      otherProperty: {
        // more properties
        date: '20170205'
      }
  }
}
其思想是递归地循环对象中的所有属性,并使用一个映射,该映射对每个应该操作的属性都有一个条目,例如

var map = new Map();
map.set('sender', getSender());
map.set('date', normalizeDate());
map.set('birthdate', normalizeDate());
对于每个属性键,将检查映射,如果它返回函数,则执行函数(如果没有) 属性被创建,循环继续


但是,我有一个明显的印象,这个问题以前已经解决了,所以我想知道是否有npm包可以替代?

在搜索了更多并尝试了我选择的不同JSON转换器之后,因为它提供了一种简单的方法,可以一次性使用多个“规则”转换部分结构

我放弃了编写大量通用规范化函数的想法,只选择了最明显的函数(性别和日期/时间),通过将各种结构转换为单个标准化结构来处理它们,然后应用适当的函数

这可能不是最优雅的方法,但我有时间限制,这是可行的

要将源转换为问题中所述的结果,使用以下代码:

const dot = require('dot-object');

const rules = {
  'header.sender.id': 'header.senderId',
  'header.receiver.id': 'header.receiverId'
};

const target = {};

dot.transform(rules, src, target);

// normalizing

target.date = normalizeDate(target.date);
target.someMore.birthdate = normalizeDate(target.someMore.birthdate);
target.someMore.otherProperty.date = normalizeDate(target.someMore.otherProperty.date);

在搜索了更多的内容并尝试了不同的JSON转换器之后,我选择了它,因为它提供了一种简单的方法,可以一次性使用多个“规则”转换部分结构

我放弃了编写大量通用规范化函数的想法,只选择了最明显的函数(性别和日期/时间),通过将各种结构转换为单个标准化结构来处理它们,然后应用适当的函数

这可能不是最优雅的方法,但我有时间限制,这是可行的

要将源转换为问题中所述的结果,使用以下代码:

const dot = require('dot-object');

const rules = {
  'header.sender.id': 'header.senderId',
  'header.receiver.id': 'header.receiverId'
};

const target = {};

dot.transform(rules, src, target);

// normalizing

target.date = normalizeDate(target.date);
target.someMore.birthdate = normalizeDate(target.someMore.birthdate);
target.someMore.otherProperty.date = normalizeDate(target.someMore.otherProperty.date);