1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

jqでJSONを整形

1
Last updated at Posted at 2018-01-09

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"
}

参考

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?