使用地图或模板在节点流中转换JSON

gre*_*mez 3 javascript json stream request node.js

我对Javascript和Node比较陌生,我喜欢边做边学,但是我对Javascript设计模式缺乏认识让我对尝试重新发明轮子很谨慎,我想从社区中了解我想要什么do已经以某种形式存在,我不是在寻找下面例子的特定代码,只是在正确的方向上轻推,我应该寻找什么.

我基本上想创建自己的私有IFTTT/Zapier,用于将数据从一个API插入另一个API.

我使用的节点模块request,以GET将数据从一个API,然后POST到另一个地方.

request 支持流媒体做这样的整洁的事情:

request.get('http://example.com/api')
  .pipe(request.put('http://example.com/api2'));
Run Code Online (Sandbox Code Playgroud)

在这两个请求之间,我想通过转换来管理JSON,选择我需要的键/值对,并将键更改为目标API所期望的.

request.get('http://example.com/api')
  .pipe(apiToApi2Map)
  .pipe(request.put('http://example.com/api2'));
Run Code Online (Sandbox Code Playgroud)

这是来自源API的JSON示例:http://pastebin.com/iKYTJCYk

这就是我要发送的内容:http://pastebin.com/133RhSJT

在这种情况下,转换后的JSON从每个对象"attribute"键的值和每个对象"value"键的值中获取键.

所以我的问题:

  • 是否有框架,库或模块可以使转换步骤更容易?

  • 流媒体的方式我应该接近这个?这似乎是一种优雅的方式,因为我已经创建了一些Javascript包装函数request来轻松访问API方法,我只需要找出中间步骤.

  • 是否可以为这些变换创建"模板"或"地图"?假设我想要更改源API或目标API,那么创建一个将源映射到所需的目标键/值的新文件会很不错.

希望社区能够提供帮助,我愿意接受任何建议!:)这是我正在开发的一个开源项目,所以如果有人愿意参与进来,请联系我们.

And*_*ers 6

是的,你肯定是在正确的轨道上.我会指向你的两个流库,through这使得定义自己的流更容易,JSONStream这有助于将二进制流(就像你得到的request.get)转换为解析的JSON文档流.以下是使用这两个方法开始的示例:

var through = require('through');
var request = require('request');
var JSONStream = require('JSONStream');
var _ = require('underscore');

// Our function(doc) here will get called to handle each
// incoming document int he attributes array of the JSON stream
var transformer = through(function(doc) {
    var steps = _.findWhere(doc.items, {
        label: "Steps"
    });
    var activeMinutes = _.findWhere(doc.items, {
        label: "Active minutes"
    });
    var stepsGoal = _.findWhere(doc.items, {
        label: "Steps goal"
    });

    // Push the transformed document into the outgoing stream
    this.queue({
        steps: steps.value,
        activeMinutes: activeMinutes.value,
        stepsGoal: stepsGoal.value
    });
});

request
    .get('http://example.com/api')
    // The attributes.* here will split the JSON stream into chunks
    // where each chunk is an element of the array
    .pipe(JSONStream.parse('attributes.*'))
    .pipe(transformer)
    .pipe(request.put('http://example.com/api2'));
Run Code Online (Sandbox Code Playgroud)