Skip to content

Instantly share code, notes, and snippets.

@nichoth
Created March 11, 2015 05:34
Show Gist options
  • Save nichoth/3fdea252671328242adb to your computer and use it in GitHub Desktop.
Save nichoth/3fdea252671328242adb to your computer and use it in GitHub Desktop.
Simple CLI for JSON transformation using Node.js streams
#!/usr/bin/env node
// Clean up JSON from openlibrary.org and output as NDJSON.
// use:
// curl 'http://openlibrary.org/subjects/love.json' | ./toNdjson.js
var JSONStream = require('JSONStream');
var map = require('through2-map');
var cherryPick = map.obj(function(obj) {
return {
key: obj.key,
title: obj.title,
authors: obj.authors,
subject: obj.subject,
};
});
process.stdin
.pipe(JSONStream.parse('works.*'))
.pipe(cherryPick)
.pipe(JSONStream.stringify(false))
.pipe(process.stdout);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment