Working with JSON output from AWSCLI

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.

import json
import sys

print "Usage: cat JSON | | 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’]

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


Hello python

I’m a Java guy and I just can’t convince myself to learn a new language – because there isn’t a better one. Let’s look at python for a second, there is visually no structure. Why is having {} blocks a problem?


# Imports
import random

s1 = "Let’s predict the next mark six!"

genEntries = 4
sysRandom = random.SystemRandom()

while genEntries > 0:
genNumbers = 17
selection = []
result = []
# first fill selection array with 17 random numbers
while genNumbers > 0:
temp = sysRandom.randint(1, 49)
if temp not in selection:
genNumbers -= 1
# then pick 6 numbers from selection
result = sysRandom.sample(selection, 6)
genEntries -= 1

Let me know if you win with this script!