質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
JSON

JSON(JavaScript Object Notation)は軽量なデータ記述言語の1つである。構文はJavaScriptをベースとしていますが、JavaScriptに限定されたものではなく、様々なソフトウェアやプログラミング言語間におけるデータの受け渡しが行えるように設計されています。

パース

パースとは、一定の文法に従って記述されたテキスト文書を解析し、データ構造の集合体に分解・変換することを呼びます。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Q&A

解決済

1回答

2962閲覧

jqコマンドを使ったaws EC2 jsonのパースについて

denden_tom

総合スコア5

JSON

JSON(JavaScript Object Notation)は軽量なデータ記述言語の1つである。構文はJavaScriptをベースとしていますが、JavaScriptに限定されたものではなく、様々なソフトウェアやプログラミング言語間におけるデータの受け渡しが行えるように設計されています。

パース

パースとは、一定の文法に従って記述されたテキスト文書を解析し、データ構造の集合体に分解・変換することを呼びます。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

0グッド

1クリップ

投稿2020/02/09 10:57

編集2020/02/10 02:14

前提・実現したいこと

あらかじめ以下のコマンドの結果をjsonファイルで保存しています。

aws ec2 describe-instances

結果のjsonは以下のようになっているとします。この時、jqを使ってInstanceIdとTags[]のKeyの値がName1のValueの値を取得したいのですが、どのようなコマンドをワンライナーで書けますでしょうか?
InstanceによってはTags[]のKeyにName1がないものもあるため、そのValueはnull(何も出力しない)としたいです。

JSON

1{ 2 "Reservations": [ 3 { 4 "ReservationId": "r-********", 5 "Groups": [], 6 "OwnerId": "************", 7 "Instances": [ 8 { 9 "PrivateDnsName": "ip-****.ap-northeast-1.compute.internal", 10 "EbsOptimized": false, 11 "Monitoring": { 12 "State": "disabled" 13 }, 14 "State": { 15 "Code": 80 16 }, 17 "NetworkInterfaces": [ 18 { 19 "PrivateDnsName": "ip-********.ap-northeast-1.compute.internal", 20 "PrivateIpAddresses": [ 21 { 22 "PrivateDnsName": "ip-****.ap-northeast-1.compute.internal", 23 "PrivateIpAddress": "172.**.**.***", 24 "Primary": true 25 } 26 ], 27 "OwnerId": "************", 28 "Groups": [ 29 { 30 "GroupName": "RDP", 31 "GroupId": "sg-********" 32 } 33 ], 34 "PrivateIpAddress": "172.**.**.***", 35 "Description": "", 36 "SourceDestCheck": true, 37 "MacAddress": "************", 38 "Status": "in-use", 39 "NetworkInterfaceId": "eni-************", 40 "VpcId": "vpc-************", 41 "SubnetId": "subnet-************" 42 } 43 ], 44 "PrivateIpAddress": "172.**.**.***", 45 "RootDeviceType": "ebs", 46 "StateTransitionReason": "User initiated (2016-02-24 10:01:43 GMT)", 47 "LaunchTime": "2016-02-24T05:53:59.000Z", 48 "Tags": [ 49 { 50 "Key": "Name", 51 "Value": "test" 52 } 53 { 54 "Key": "Name1", 55 "Value": "test1" 56 } 57 { 58 "Key": "Name2", 59 "Value": "test2" 60 } 61 ], 62 "SubnetId": "subnet-************", 63 "Platform": "windows", 64 "StateReason": { 65 "Message": "Client.UserInitiatedShutdown: User initiated shutdown", 66 "Code": "Client.UserInitiatedShutdown" 67 }, 68 "InstanceId": "i-***********1", 69 "SourceDestCheck": true, 70 "ClientToken": "rduVq************", 71 "AmiLaunchIndex": 0 72 } 73 ] 74 "Instances": [ 75 { 76 "PrivateDnsName": "ip-****.ap-northeast-1.compute.internal", 77 "EbsOptimized": false, 78 "Monitoring": { 79 "State": "disabled" 80 }, 81 "State": { 82 "Code": 80 83 }, 84 "NetworkInterfaces": [ 85 { 86 "PrivateDnsName": "ip-********.ap-northeast-1.compute.internal", 87 "PrivateIpAddresses": [ 88 { 89 "PrivateDnsName": "ip-****.ap-northeast-1.compute.internal", 90 "PrivateIpAddress": "172.**.**.***", 91 "Primary": true 92 } 93 ], 94 "OwnerId": "************", 95 "Groups": [ 96 { 97 "GroupName": "RDP", 98 "GroupId": "sg-********" 99 } 100 ], 101 "PrivateIpAddress": "172.**.**.***", 102 "Description": "", 103 "SourceDestCheck": true, 104 "MacAddress": "************", 105 "Status": "in-use", 106 "NetworkInterfaceId": "eni-************", 107 "VpcId": "vpc-************", 108 "SubnetId": "subnet-************" 109 } 110 ], 111 "PrivateIpAddress": "172.**.**.***", 112 "RootDeviceType": "ebs", 113 "StateTransitionReason": "User initiated (2016-02-24 10:01:43 GMT)", 114 "LaunchTime": "2016-02-24T05:53:59.000Z", 115 "Tags": [ 116 { 117 "Key": "Name", 118 "Value": "test" 119 } 120 { 121 "Key": "Name3", 122 "Value": "test3" 123 } 124 ], 125 "SubnetId": "subnet-************", 126 "Platform": "windows", 127 "StateReason": { 128 "Message": "Client.UserInitiatedShutdown: User initiated shutdown", 129 "Code": "Client.UserInitiatedShutdown" 130 }, 131 "InstanceId": "i-***********2", 132 "SourceDestCheck": true, 133 "ClientToken": "rduVq************", 134 "AmiLaunchIndex": 0 135 } 136 ] 137 "Instances": [ 138 { 139 "PrivateDnsName": "ip-****.ap-northeast-1.compute.internal", 140 "EbsOptimized": false, 141 "Monitoring": { 142 "State": "disabled" 143 }, 144 "State": { 145 "Code": 80 146 }, 147 "NetworkInterfaces": [ 148 { 149 "PrivateDnsName": "ip-********.ap-northeast-1.compute.internal", 150 "PrivateIpAddresses": [ 151 { 152 "PrivateDnsName": "ip-****.ap-northeast-1.compute.internal", 153 "PrivateIpAddress": "172.**.**.***", 154 "Primary": true 155 } 156 ], 157 "OwnerId": "************", 158 "Groups": [ 159 { 160 "GroupName": "RDP", 161 "GroupId": "sg-********" 162 } 163 ], 164 "PrivateIpAddress": "172.**.**.***", 165 "Description": "", 166 "SourceDestCheck": true, 167 "MacAddress": "************", 168 "Status": "in-use", 169 "NetworkInterfaceId": "eni-************", 170 "VpcId": "vpc-************", 171 "SubnetId": "subnet-************" 172 } 173 ], 174 "PrivateIpAddress": "172.**.**.***", 175 "RootDeviceType": "ebs", 176 "StateTransitionReason": "User initiated (2016-02-24 10:01:43 GMT)", 177 "LaunchTime": "2016-02-24T05:53:59.000Z", 178 "Tags": [ 179 { 180 "Key": "Name", 181 "Value": "test" 182 } 183 { 184 "Key": "Name1", 185 "Value": "test1" 186 } 187 { 188 "Key": "Name4", 189 "Value": "test4" 190 } 191 ], 192 "SubnetId": "subnet-************", 193 "Platform": "windows", 194 "StateReason": { 195 "Message": "Client.UserInitiatedShutdown: User initiated shutdown", 196 "Code": "Client.UserInitiatedShutdown" 197 }, 198 "InstanceId": "i-***********3", 199 "SourceDestCheck": true, 200 "ClientToken": "rduVq************", 201 "AmiLaunchIndex": 0 202 } 203 ] 204 } 205 ] 206}

発生している問題・エラーメッセージ

事前に取得したjsonファイルのファイル名をxxx.jsonとして、以下のコマンドを試しましたが、エラーが発生します。
当方の環境はpowershell6でございます。

Get-Content xxx.json | jq -r '.Reservations[].Instances[] |.InstanceId, .Tags[] | select(.Key == "Name1")'

<エラー内容>

jq: error: Name1/0 is not defined at <top-level>, line 1: .Reservations[].Instances[] |.InstanceId, .Tags[] | select(.Key == Name1) jq: 1 compile error

想定している結果

想定している結果は以下の通りです。i-***********2のTags[]のKeyにName1はないためValueは出力しないようにしたいです。
i-***********1,test1
i-***********2,
i-***********3,test1

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

otn

2020/02/09 14:42

例示は、正しいJSON形式のファイルを書いてください。
denden_tom

2020/02/10 02:15

otn様 JSONファイルの内容を修正しました。ご確認いただけますでしょうか?
otn

2020/02/10 16:07

やっぱり不正なJSONだと思いますが、本当にこれでエラーにならないのでしょうか? jq-1.6 だと、不正なJSONとしてエラーが出ます。エラー箇所を見るとカンマが抜けているようです。
guest

回答1

0

自己解決

こちら自己解決しました。
Powershellの場合、この場合だとエラーになってしまいます。
Get-Content xxx.json | jq -r '.Reservations[].Instances[] |.InstanceId, .Tags[] | select(.Key == "Name1")'

以下のようにすることで解決しました。
Get-Content xxx.json | jq -r '.Reservations[].Instances[] |.InstanceId, .Tags[] | select(.Key == "Name1")'

投稿2020/02/10 05:01

denden_tom

総合スコア5

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問