Reputation: 1272
I am trying to retrieve a key value from aws cli response via jq in shell script however not getting the intended response.
could you please help figure how to retrieve the desired key in below condition
resource=aws lambda add-permission --function-name $functionName --statement-id "testing" --action "lambda:InvokeFunction" --principal "events.amazonaws.com" --source-arn $rulearn
echo ${resource} | jq ".[]"
this return me a response like
{\"Sid\":\"xxxxxxxx\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"events.amazonaws.com\"},\"Action\":\"lambda:InvokeFunction\",\"Resource\":\"arn:aws:lambda:us-west-2:xxxxxxxx:function:xxxxxx\",\"Condition\":{\"ArnLike\":{\"AWS:SourceArn\":\"arn:aws:events:us-west-2:xxxxxx:rule/xxxxx\"}}}"
I am trying to get value of resource using below line
echo ${resource} | jq ".[]" | jq ".Resource"
however response is coming back as
[
133
]
What should be the correct way to retrieve value of key resource.
Upvotes: 0
Views: 580
Reputation: 1272
I made it work like below:-
Made resource outputted in text format
resource=$(aws lambda add-permission --function-name $functionName --statement-id "xxxxxx" --action "lambda:InvokeFunction" --principal "events.amazonaws.com" --source-arn $rulearn --query 'Statement' --output text )
and then converted it to json using below and then retreived resource
resource=$(echo ${resource} | jq "." | jq ".Resource" | sed "s|\"||g")
Upvotes: 0
Reputation: 116870
The JSON included in the question is a JSON string (except that the initial double-quote is missing). To convert it into a JSON object, you can use the fromjson
filter, so you would write:
jq 'fromjson|.Resource'
With the given JSON string (with the initial double-quote) as input, this produces:
"arn:aws:lambda:us-west-2:xxxxxxxx:function:xxxxxx"
Upvotes: 1
Reputation: 4675
The Resource
field seems to be in the root object so running echo ${resource} | jq ".Resource"
should do the trick
Upvotes: 0