Menu
blog.headdesk.me
blog.headdesk.me

Working with JSON output from AWSCLI

Posted on 2014/03/252015/02/02

The program looks short but the way Python handles JSON is not very elegant. JSON is supposed to be a serialized object, so it should be processed as an object. But here, it’s processed as an array.

[python]
#!/usr/bin/python
import json
import sys

print "Usage: cat JSON | BasicParse.py | column -t"
jsonInput = sys.stdin
awsObject = json.load(jsonInput)

instances = awsObject[‘Reservations’]

for instance in instances:
print instance[‘Instances’][0][‘Tags’][0][‘Value’], instance[‘Instances’][0][‘InstanceId’], instance[‘Instances’][0][‘PublicDnsName’]
[/python]

Not only that, there is no type definition with any of the variables. I have no idea what was returned by json.load.

Also, exception handling is quite poor. I tried to do a try-except block but the program got stuck with no stdin was piped. If I try to read from stdin and calculate the length, the read line is consumed and I couldn’t go back.

Anyway, this program will take output from awscli (json format) and extract 3 fields that I want.

On a relevant note, it is also possible to use the –query option to narrow down the search fields. For example

$ aws ec2 describe-volumes --output=text --query 'Volumes[*].{ID:VolumeId,Size:Size}'
vol-d74xxx93	30
vol-784xxx3c	30
vol-854xxxc1	60

 

Loading

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Full text search

Recent Posts

  • Error 0xc1900101 when updating to Windows 11 22H2
  • Apply changes to all member accounts in an AWS organization
  • RPI4 under voltage
  • Upgrade RockyLinux 8 to 9
  • Terraform and segregated permissions
  • aws (9)
  • coffee (1)
  • headfi (1)
  • linux (8)
  • others (57)
  • security (2)
  • tech (39)
  • wordpress (2)

Loading

apache apigateway aws awscli azure backup cloud coffee docker ec2 EL8 ElasticBeanstalk enpass espresso featured kernel lelit linux lvm meltdown MFA mikrotik nat gateway nginx php power python rdp Redhat RHEL rpm Ryzen s2s scp serverless site-to-site snapshot spectre tech terraform ubuntu ubuntu upgrade vpn wordpress xdotool

©2023 blog.headdesk.me | Powered by SuperbThemes