I’m running a call to AWS CLI: aws ec2 describe-volumes --query Volumes[*].{ID:VolumeId}
which returns json:
[ { "ID": "vol-123456789101112" } ]
I’m calling it from python’s subprocess function:
command = subprocess.run('aws ec2 describe-volumes --query Volumes[*].{ID:VolumeId}', shell=True, capture_output=True) output = command.stdout
Which outputs: b'[rn {rn "ID": "vol-123456789101112"rn }rn]rn'
Is there a better approach to collecting this json, then making it easier to parse and collect the individual IDs if several are returned.
Answer
subprocess
returns bytes
by default on Python 3. If you expect text, try adding text=True
to the keyword arguments. (json
will accept bytes
just fine, but if you want to inspect the result before parsing it, you can easily get rid of the unnerving b
prefix.)
command = subprocess.run( ['aws', 'ec2', 'describe-volumes', '--query', 'Volumes[*].{ID:VolumeId}'], check=True, text=True, capture_output=True)
Notice also the addition of check=True
and the refactoring to remove the unnecessary and wasteful shell=True
.