usememos.yammer.jpRSS

世の中の多くのシチュエーションでjqでいいじゃん、となるのだけど、Node.jsで代替できないかとちょっと考えてみた。

$ curl https://rsss.yammer.jp/v0/json_feed > /tmp/stdin.json && node -e 'console.log(require("/tmp/stdin.json"))'

このままだと長いオブジェクトは省略されてしまうので、JSON.stringifyを使う。第三引数に空白を入れると、改行を含んだフォーマットで出力してくれるらしい。

$ curl https://rsss.yammer.jp/v0/json_feed > /tmp/stdin.json && node -e 'console.log(JSON.stringify(require("/tmp/stdin.json"), null, " "))'

もうちょっと進化させて、以下のようなシェルスクリプトを作った。

#!/bin/bash -e

cat /dev/stdin > /tmp/stdin.json

SCRIPT="$*"

node << CODE
'use strict'

let input = require("/tmp/stdin.json");
let output = $SCRIPT
;
console.log(JSON.stringify(output, null, " "));
CODE

このファイルに実行権限を持たせてPATHを通しておく。たとえばjnという名前をつけた場合、次のように使える。

$ curl https://rsss.yammer.jp/v0/json_feed | ./jn 'input.items.map(e => e.url)'

これは普通にJavaScriptなので、jqの記法を忘れたなどと困ることはない。