title: concise way to transform a node.js stream?
body: <p>I'm trying to convert a newline-separated JSON file to CSV (kind of a dumb task, but it doesn't matter), and am curious how I can easily do this in <strong>node.js</strong>. What I have so far is,</p>
  
  <pre><code>var csv = require('csv'), 
      fs = require('fs'),
      JSONStream = require('JSONStream');
  
  var stream = fs.createReadStream('input.json', {encoding: 'utf8'})
      .pipe(JSONStream.parse());
  csv().from(stream).to('out.csv');
  </code></pre>
  
  <p>This works, but doesn't let me express ordering on the keys. For example, if I have an input file like this,</p>
  
  <pre><code>{"foo": 1, "bar": 2}
  {"bar": 3, "foo": -1}
  </code></pre>
  
  <p>I'd like to add a tranformer function,</p>
  
  <pre><code>function(line_object) { return [line_object.foo, line_object.bar]; };
  </code></pre>
  
  <p>so that my output will be lines <code>1, 2</code> and <code>-1, 3</code>, corresponding to <code>foo</code> being column 1 and <code>bar</code> being column 2, instead of the current behavior <code>1, 2</code> and <code>3, -1</code>.</p>
  
  <p>However, all of the examples online seem rather verbose for adding transformers. They seem to create <code>require('stream').Transform</code> objects, and then set the <code>_transform</code> function on those objects. <em>Question</em>: Is there a cleaner / more concise way to do this? Thank you very much, and please forgive my ignorance as this is pretty much the first <code>node.js</code> code I've written.</p>