JSON
jq

jqでJSONを整形

REST API等でGETしたJSONの特定値のみを取得したい場合に役立つjq

準備

Linux(CentOS/AmazonLinux)にjqをインストール

wget http://stedolan.github.io/jq/download/linux64/jq
chmod 755 jq
sudo mv jq /usr/local/bin/

or

yum install -y  jq

jqコマンドを実行して以下となればOK

$ jq

jq - commandline JSON processor [version 1.4]
Usage: jq [options] <jq filter> [file...]

For a description of the command line options and
how to write jq filters (and why you might want to)
see the jq manpage, or the online documentation at
http://stedolan.github.com/jq

整形

例えばこんなJSONを整形
※サンプルとしてKMSの鍵作成結果(aws kms create-key)をexample.jsonファイルとする(結果はAWS Document値)

{
    "KeyMetadata": {
        "Origin": "AWS_KMS",
        "KeyId": "1234abcd-12ab-34cd-56ef-1234567890ab",
        "Description": "",
        "KeyManager": "CUSTOMER",
        "Enabled": true,
        "KeyUsage": "ENCRYPT_DECRYPT",
        "KeyState": "Enabled",
        "CreationDate": 1502910355.475,
        "Arn": "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab",
        "AWSAccountId": "111122223333"
    }
}

[1] 特定key配下を取得したい場合

$cat example.json | jq  '.KeyMetadata'

{
  "Origin": "AWS_KMS",
  "KeyId": "1234abcd-12ab-34cd-56ef-1234567890ab",
  "Description": "",
  "KeyManager": "CUSTOMER",
  "Enabled": true,
  "KeyUsage": "ENCRYPT_DECRYPT",
  "KeyState": "Enabled",
  "CreationDate": 1502910355.475,
  "Arn": "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab",
  "AWSAccountId": "111122223333"
}

[2] さらに値だけ取得したい場合

$cat example.json | jq  '.KeyMetadata[]'

"AWS_KMS"
"1234abcd-12ab-34cd-56ef-1234567890ab"
""
"CUSTOMER"
true
"ENCRYPT_DECRYPT"
"Enabled"
1502910355.475
"arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"
"111122223333"

[3] 入れ子の特定キーの値だけ取得したい場合

-rオプションで " を除外できる

$cat example.json | jq -r '.KeyMetadata | .KeyId'


1234abcd-12ab-34cd-56ef-1234567890ab

[4]前方一致で特定文字を含むものを抽出

$cat example.json | jq '.[] | select(.KeyManager | startswith("CUS"))'

  "Origin": "AWS_KMS",
  "KeyId": "1234abcd-12ab-34cd-56ef-1234567890ab",
  "Description": "",
  "KeyManager": "CUSTOMER",
  "Enabled": true,
  "KeyUsage": "ENCRYPT_DECRYPT",
  "KeyState": "Enabled",
  "CreationDate": 1502910355.475,
  "Arn": "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab",
  "AWSAccountId": "111122223333"

[5]完全一致で特定文字を含むものを抽出

$cat example.json |
jq '.[] | select(.KeyManager=="CUSTOMER")'
{
  "Origin": "AWS_KMS",
  "KeyId": "1234abcd-12ab-34cd-56ef-1234567890ab",
  "Description": "",
  "KeyManager": "CUSTOMER",
  "Enabled": true,
  "KeyUsage": "ENCRYPT_DECRYPT",
  "KeyState": "Enabled",
  "CreationDate": 1502910355.475,
  "Arn": "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab",
  "AWSAccountId": "111122223333"
}

参考

https://dev.classmethod.jp/tool/road-to-jq-master-apprentice/