These are chat archives for aws/aws-cli

21st
Mar 2018
DevilWAH
@DevilWAH
Mar 21 16:40
Hi, hoping some one can help, I have this CLI command "aws ec2 describe-images --filter "Name=description,Values=Auto backed up on *" --query 'Images[].{Description:Description,Image_ID:ImageId,DateCreated:CreationDate}'" that I want parse using a bash script
it out puts to screen {
"Image_ID": "ami-xxxxxx,
"Description": "Auto backed up on Fri Mar 16 00:05:00 UTC 2018",
"DateCreated": "2018-03-16T00:05:02.000Z"
},
{
"Image_ID": "ami-xxxxxxx",
"Description": "Auto backed up on Fri Mar 16 00:05:03 UTC 2018",
"DateCreated": "2018-03-16T00:05:05.000Z"
},
but if i save as a variable its just single words, how can I save json output to an array
David M. Karr (fullname at gmail.com)
@davidmichaelkarr
Mar 21 16:46
@DevilWAH Use "jq".
DevilWAH
@DevilWAH
Mar 21 16:47
OK let me have a llok
I had wanted to do it in raw shell so the script is portable.
Randall Kahler
@angrychimp
Mar 21 17:59
jq is great, but not for piping output to another bash script
David M. Karr (fullname at gmail.com)
@davidmichaelkarr
Mar 21 18:00
It's more powerful when you use it to extract specific values.
Randall Kahler
@angrychimp
Mar 21 18:00
@DevilWAH you can use --output text to flatten the output, then potentially pipe to awk to grab individual columns if you'd like. Alternatively you can change $IFS to a newline to parse multiple lines at a time
jq certainly can be more powerful, but might not be as easy as just robust jmespath queries
@DevilWAH to answer your exact question, it's not easy to take structured, multi-line JSON and pass it to a bash script via pipe. You'd need to pass the full content to a script and some how decode the JSON into something your bash script could handle. You're better off with a different language like Python or Perl, which has available JSON libraries, or using --output text as I mentioned above
Randall Kahler
@angrychimp
Mar 21 18:16
I'm not sure if it's documented anywhere (it very well may be, I just haven't looked), but when using --output text the CLI seems to order columns alphabetically by the name you provide. So my snippet above assumes the order you provided in the example. You could rearrange the columns using other names (or numerical values).