Home > Net >  Saving JSON to a variable then parsing with JQ
Saving JSON to a variable then parsing with JQ

Time:08-15

I'm trying to return json from an AWS cli call, store it in a variable (so that I can use it for multiple steps) and then extract a property from it with jq.

#!/bin/bash
TARGETARN="arn:aws:iam::11111111111:role/OrganizationAccountAccessRole"
COMMAND="aws sts assume-role --role-arn $TARGETARN --role-session-name my_session"
RESULT=$($COMMAND) 
AKI=$($RESULT | jq -r '.Credentials.AccessKeyId')

It would appear that AWS is returning formatted json. ECHO $RESULT appears to output as I'd expect, showing clean JSON. However, when I pipe $RESULT to jq it appears to have been formatted with single quotes around braces. eg,

'{' Credentials: '{' AccessKeyId: ASI.......

This in turn appears to be throwing off jq. I'm not sure which stage above is introducing the single quotes.

Can someone please point me in the right direction?

Thanks

CodePudding user response:

You don't feed your string into jq in your code. Since you are using bash, this could be done by

AKI=$(jq -r '.Credentials.AccessKeyId' <<<$RESULT)
  • Related