jqを使用してキーと値を印刷する方法

jqを使用してキーと値を印刷する方法

私はjqを初めて使用し、選択したフィールドからキーと値を抽出したいと思います。

{
    "StackEvents": [
        {
            "StackId": "arn:aws:cloudformation:us-east-1:1234567890123:stack/jenkinscf-s3/f64657b0-cb0a-11ed-848f-0a093f43c24f",
            "EventId": "06263740-cb0b-11ed-86ee-0e3b4b6ffec3",
            "StackName": "jenkinscf-s3",
            "LogicalResourceId": "jenkinscf-s3",
            "PhysicalResourceId": "arn:aws:cloudformation:us-east-1:1234567890123:stack/jenkinscf-s3/f64657b0-cb0a-11ed-848f-0a093f43c24f",
            "ResourceType": "AWS::CloudFormation::Stack",
            "Timestamp": "2023-03-25T12:46:12.647000+00:00",
            "ResourceStatus": "CREATE_COMPLETE"
        },
        {
            "StackId": "arn:aws:cloudformation:us-east-1:1234567890123:stack/jenkinscf-s3/f64657b0-cb0a-11ed-848f-0a093f43c24f",
            "EventId": "S3Bucket-CREATE_COMPLETE-2023-03-25T12:46:11.127Z",
            "StackName": "jenkinscf-s3",
            "LogicalResourceId": "S3Bucket",
            "PhysicalResourceId": "jenkinscf-s3-s3bucket-tmh6orxmeegs",
            "ResourceType": "AWS::S3::Bucket",
            "Timestamp": "2023-03-25T12:46:11.127000+00:00",
            "ResourceStatus": "CREATE_COMPLETE",
            "ResourceProperties": "{}"
        },
        {
            "StackId": "arn:aws:cloudformation:us-east-1:1234567890123:stack/jenkinscf-s3/f64657b0-cb0a-11ed-848f-0a093f43c24f",
            "EventId": "S3Bucket-CREATE_IN_PROGRESS-2023-03-25T12:45:50.261Z",
            "StackName": "jenkinscf-s3",
            "LogicalResourceId": "S3Bucket",
            "PhysicalResourceId": "jenkinscf-s3-s3bucket-tmh6orxmeegs",
            "ResourceType": "AWS::S3::Bucket",
            "Timestamp": "2023-03-25T12:45:50.261000+00:00",
            "ResourceStatus": "CREATE_IN_PROGRESS",
            "ResourceStatusReason": "Resource creation Initiated",
            "ResourceProperties": "{}"
        },
        {
            "StackId": "arn:aws:cloudformation:us-east-1:1234567890123:stack/jenkinscf-s3/f64657b0-cb0a-11ed-848f-0a093f43c24f",
            "EventId": "S3Bucket-CREATE_IN_PROGRESS-2023-03-25T12:45:49.665Z",
            "StackName": "jenkinscf-s3",
            "LogicalResourceId": "S3Bucket",
            "PhysicalResourceId": "",
            "ResourceType": "AWS::S3::Bucket",
            "Timestamp": "2023-03-25T12:45:49.665000+00:00",
            "ResourceStatus": "CREATE_IN_PROGRESS",
            "ResourceProperties": "{}"
        },
        {
            "StackId": "arn:aws:cloudformation:us-east-1:1234567890123:stack/jenkinscf-s3/f64657b0-cb0a-11ed-848f-0a093f43c24f",
            "EventId": "f64b87d0-cb0a-11ed-848f-0a093f43c24f",
            "StackName": "jenkinscf-s3",
            "LogicalResourceId": "jenkinscf-s3",
            "PhysicalResourceId": "arn:aws:cloudformation:us-east-1:1234567890123:stack/jenkinscf-s3/f64657b0-cb0a-11ed-848f-0a093f43c24f",
            "ResourceType": "AWS::CloudFormation::Stack",
            "Timestamp": "2023-03-25T12:45:46.136000+00:00",
            "ResourceStatus": "CREATE_IN_PROGRESS",
            "ResourceStatusReason": "User Initiated"
        }
    ]
}

期待される出力

"LogicalResourceId": "jenkinscf-s3"
"ResourceStatus": "CREATE_COMPLETE"

私が試したソリューションはキーまたは値のみを取得できますが、両方を取得することはできません。

.StackEvents | .[] | .LogicalResourceId, .ResourceStatus
"jenkinscf-s3"
"CREATE_COMPLETE"
"S3Bucket"
"CREATE_COMPLETE"
"S3Bucket"
"CREATE_IN_PROGRESS"
"S3Bucket"
"CREATE_IN_PROGRESS"
"jenkinscf-s3"
"CREATE_IN_PROGRESS"
.StackEvents[] | keys
[
  "EventId",
  "LogicalResourceId",
  "PhysicalResourceId",
  "ResourceStatus",
  "ResourceType",
  "StackId",
  "StackName",
  "Timestamp"
]

私の究極の目標は、これをbashスクリプトで使用することです。このスクリプトはスタックのドリフトを検出し、違いを出力します。現在は実行され、値だけを出力するので、スクリプトに慣れていない人はキー自体を見るだけで簡単に混乱する可能性があります。

答え1

クイックsedソリューション:

$ sed -n 's/^ *"\(LogicalResourceId\|ResourceStatus\)": "\([^"]*\)",*/\1: \2/p' input.json
LogicalResourceId: jenkinscf-s3
ResourceStatus: CREATE_COMPLETE
LogicalResourceId: S3Bucket
ResourceStatus: CREATE_COMPLETE
LogicalResourceId: S3Bucket
ResourceStatus: CREATE_IN_PROGRESS
LogicalResourceId: S3Bucket
ResourceStatus: CREATE_IN_PROGRESS
LogicalResourceId: jenkinscf-s3
ResourceStatus: CREATE_IN_PROGRESS

目的の出力とは異なり、引用符はありませんが、この出力が後でbashスクリプトによって処理される予定の場合は、なぜ引用符が必要なのかわかりません。ただし、これを行うと、sed式の代替部分に\1andを引用符で囲むことができます。\2

jqそれも簡単です:

$ jq -r '.StackEvents[] | "LogicalResourceId: \(.LogicalResourceId)", "ResourceStatus: \(.ResourceStatus)"' input.json
LogicalResourceId: jenkinscf-s3
ResourceStatus: CREATE_COMPLETE
LogicalResourceId: S3Bucket
ResourceStatus: CREATE_COMPLETE
LogicalResourceId: S3Bucket
ResourceStatus: CREATE_IN_PROGRESS
LogicalResourceId: S3Bucket
ResourceStatus: CREATE_IN_PROGRESS
LogicalResourceId: jenkinscf-s3
ResourceStatus: CREATE_IN_PROGRESS

必要に応じてここに引用符を追加するのは簡単ですが、バックスラッシュでエスケープする必要があります。

答え2

jq次のように使用できますawk

#!/bin/bash

input=$(cat input.json)

output=$(echo "$input" | jq -r '.StackEvents[] | {LogicalResourceId, ResourceStatus}')
echo "$output" | awk '{gsub(/[{}]/,"")}NF'

jq出力:

{LogicalResourceId": "jenkinscf-s3",
 ResourceStatus": "CREATE_COMPLETE"}
{"LogicalResourceId": "S3Bucket",
 "ResourceStatus": "CREATE_COMPLETE"}

...など

awkは削除し{}て空白行を作成します。

最終出力は次のとおりです。

  "LogicalResourceId": "jenkinscf-s3",
  "ResourceStatus": "CREATE_COMPLETE"
  "LogicalResourceId": "S3Bucket",
  "ResourceStatus": "CREATE_COMPLETE"
  "LogicalResourceId": "S3Bucket",
  "ResourceStatus": "CREATE_IN_PROGRESS"
  "LogicalResourceId": "S3Bucket",
  "ResourceStatus": "CREATE_IN_PROGRESS"
  "LogicalResourceId": "jenkinscf-s3",
  "ResourceStatus": "CREATE_IN_PROGRESS"

関連情報