LoginSignup
6
6

More than 5 years have passed since last update.

aws-cliはあるがjqがない場合につかえる jp.py

Posted at

はじめに

まれにですが、aws-cliはあるのにjqが使えないという環境があります。 jqをいれればよいではないかという指摘はもっともなのですが、そうもいかない場合があるので、そんなときに使えるjp.pyを説明します。

JAWSUG CLI支部 のテキストで知りました。

JMESPathとjp.py について

JSON用のクエリ言語 JMESPathのpython実装がjp.pyです。
JMESPathは aws-cliの query オプションでも利用できます。
jp.pyはaws-cliをインストールすると一緒にインストールされ、かつ、単体のコマンドで実行可能な点が嬉しいです。
JMESPathについては チュートリアル(英語) も参考になります。
jqと目的は一緒ですが、言語が違います。

使い方

aws-cliで取得したインスタンス一覧から、jp.pyでインスタンスIDを取得します。

$ aws --region ap-northeast-1 ec2 describe-instances |  jp.py 'Reservations[].Instances[].InstanceId'
[
    "i-297dXXXX",
    "i-c0e2XXXX",
    "i-02965fbeXXXXXXX"
]

jp.py --help みればわかるとおりシンプルです。

$ jp.py --help
usage: jp.py [-h] [-f FILENAME] [--ast] expression

positional arguments:
  expression

optional arguments:
  -h, --help            show this help message and exit
  -f FILENAME, --filename FILENAME
                        The filename containing the input data. If a filename
                        is not given then data is read from stdin.
  --ast                 Pretty print the AST, do not search the data.

jp.py の悪い点

ググらビリティが低すぎる。。。。

参考

以下、とても参考になりました。ありがとうございます。

【個人メモ】JSONのパースにjmespathを使ってみよう
aws-cli で使える –query = jmespath をガッツリ使い込む
Python版AWS CLI 1.3の強化されたqueryオプションを使いこなすスニペット4つ

6
6
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
6
6