LoginSignup
17
13

More than 5 years have passed since last update.

jq だけじゃない JSON ツールのご紹介

Posted at

はじめに

JSON を操作するツールと言えば jq がよく紹介されますが、要素検索のための構文を覚える必要があり、使いこなすまでにはある程度学習しておく必要があります。そこで今回は jq の構文を知らなくても要素検索ができる JSON ツールを 2 つご紹介したいと思います。

ご紹介するツールは gronfx というツールになりますが、これらは jq と比較して JSON の要素検索の仕方に次のような違いがあります。

ツール 要素検索
jq 専用の構文を使用
gron grep を使用
fx 対話的 or JavaScript 構文を使用

動作確認した環境

  • Linux

JSON 操作の動作確認に便利なサイト

ツールの動作確認では httpbin というサイトを使用します。httpbin は一種のモックサーバで、様々な HTTP リクエストを投げるとそれに応じたレスポンスを返してくれるというサイトです。httpbin の中に下記のような JSON を返却してくれるリクエストがあるので、それを使用したいと思います。

$ curl -s https://httpbin.org/json
{
  "slideshow": {
    "author": "Yours Truly",
    "date": "date of publication",
    "slides": [
      {
        "title": "Wake up to WonderWidgets!",
        "type": "all"
      },
      {
        "items": [
          "Why <em>WonderWidgets</em> are great",
          "Who <em>buys</em> WonderWidgets"
        ],
        "title": "Overview",
        "type": "all"
      }
    ],
    "title": "Sample Slide Show"
  }
}

gron

gron は JSON の各要素を 1 行ごとに出力してくれるツールです。例えば gronhttps://httpbin.org/json を渡すと次のような出力結果が得られます。

$ gron https://httpbin.org/json
json = {};
json.slideshow = {};
json.slideshow.author = "Yours Truly";
json.slideshow.date = "date of publication";
json.slideshow.slides = [];
json.slideshow.slides[0] = {};
json.slideshow.slides[0].title = "Wake up to WonderWidgets!";
json.slideshow.slides[0].type = "all";
json.slideshow.slides[1] = {};
json.slideshow.slides[1].items = [];
json.slideshow.slides[1].items[0] = "Why <em>WonderWidgets</em> are great";
json.slideshow.slides[1].items[1] = "Who <em>buys</em> WonderWidgets";
json.slideshow.slides[1].title = "Overview";
json.slideshow.slides[1].type = "all";
json.slideshow.title = "Sample Slide Show";

ここで例えば値が "all" の要素だけ抽出したいときは grep にパイプすることで得ることができます。

$ gron https://httpbin.org/json | grep all
json.slideshow.slides[0].type = "all";
json.slideshow.slides[1].type = "all";

さらに gron --ungron というコマンドでこの結果を JSON に逆変換することができます。

$ gron https://httpbin.org/json | grep all | gron --ungron
{
  "slideshow": {
    "slides": [
      {
        "type": "all"
      },
      {
        "type": "all"
      }
    ]
  }
}

grongrep と組み合わせるだけで簡単に要素の抽出ができます。覚えることが少なくてすぐに使用することができるため、おすすめです。また gron の実行結果は JavaScript の構文で書かれているため .js ファイルに保存すれば JavaScript コードとして使用することもできます。

fx

fx はターミナル上で使用できる JSON ビューアです。curl などで取得した JSON をパイプで fx に渡して閲覧するというのが一般的な使い方になります。

fx は次のような機能を備えています。

  • マウスクリックでオブジェクトの展開・折りたたみ
  • キー名でフィルタリング
  • キー名の補完機能

ビューアとしてだけではなく、jq のような要素検索ツールとしても使用できます。要素の検索には JavaScript の構文が使用できます。

$ curl -s https://httpbin.org/json | fx 'Object.keys(this.slideshow)'
[
  "author",
  "date",
  "slides",
  "title"
]

.fxrc という設定ファイルをホームディレクトリに作成しておくと fx の実行時にその設定を有効にしてくれます。例えば Lodashfx で使用したければ下記のように .fxrc を定義しておくことで使用できるようになります。

.fxrc
Object.assign(global, require('lodash'));
$ npm install -g lodash
$ export NODE_PATH=$(npm root -g)
$ curl -s https://httpbin.org/json | fx '_.keys(this.slideshow)'
[
  "author",
  "date",
  "slides",
  "title"
]

自作した関数を .fxrc 内に定義して使用することもできます。fx は JSON 操作を JavaScript の構文で実現するので JavaScript の操作に慣れている方であれば jq よりも使いやすい面もあるのではないかと思います。

まとめ

jq 以外の JSON ツールを紹介してみました。状況に応じて使い分けると便利だと思うのでぜひお試し下さい。

17
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
13