ページ

2013/05/13

コマンドラインJSONプロセッサ jq をインスールする

JSON をフォーマットしてくれたり抽出したりしてくれるすごいやつです。

インストール方法は Download jq にあるように
brew install jqとすればできるようなのだけどエラーで失敗するので
バイナリの方をダウンロードしてきてchmod +x jqで実行権限を付与して
パスの通ったところに置けば使えます。

簡単に使い方を説明すると以下のような JSON あったとして、
{"Exception":{"stacktrace":["at [SplashActivity#onCreate:46]","at [Instrumentation#callActivityOnCreate:1047]","at [ActivityThread#performLaunchActivity:1623]","at [ActivityThread#handleLaunchActivity:1675]","at [ActivityThread#access$1500:121]","at [ActivityThread$H#handleMessage:943]","at [Handler#dispatchMessage:99]","at [Looper#loop:130]","at [ActivityThread#main:3701]","at [Method#invokeNative:-2]","at [Method#invoke:507]","at [ZygoteInit$MethodAndArgsCaller#run:866]","at [ZygoteInit#main:624]","at [NativeStart#main:-2]"],"name":"java.lang.OutOfMemoryError"},"PackageInfo":{"packageName":"com.wada811.devcamera","versionCode":1,"versionName":"1.0"},"SharedPreferences":{"isLatestversion":true},"Build":{"VERSION.RELEASE":"2.3.4","DEVICE":"SO-01C","MODEL":"SO-01C","MANUFACTURER":"Sony Ericsson","BRAND":"docomo","VERSION.SDK_INT":10}}
これをjq . report.jsonで以下のように表示することができます。
{
  "Build": {
    "VERSION.SDK_INT": 10,
    "BRAND": "docomo",
    "MANUFACTURER": "Sony Ericsson",
    "MODEL": "SO-01C",
    "DEVICE": "SO-01C",
    "VERSION.RELEASE": "2.3.4"
  },
  "SharedPreferences": {
    "isLatestversion": true
  },
  "PackageInfo": {
    "versionName": "1.0",
    "versionCode": 1,
    "packageName": "com.wada811.devcamera"
  },
  "Exception": {
    "name": "java.lang.OutOfMemoryError",
    "stacktrace": [
      "at [SplashActivity#onCreate:46]",
      "at [Instrumentation#callActivityOnCreate:1047]",
      "at [ActivityThread#performLaunchActivity:1623]",
      "at [ActivityThread#handleLaunchActivity:1675]",
      "at [ActivityThread#access$1500:121]",
      "at [ActivityThread$H#handleMessage:943]",
      "at [Handler#dispatchMessage:99]",
      "at [Looper#loop:130]",
      "at [ActivityThread#main:3701]",
      "at [Method#invokeNative:-2]",
      "at [Method#invoke:507]",
      "at [ZygoteInit$MethodAndArgsCaller#run:866]",
      "at [ZygoteInit#main:624]",
      "at [NativeStart#main:-2]"
    ]
  }
}
すごい!
詳しい使い方は jq Manual を見てください。