Creating Json Output Boto3 Creating Json Output Boto3 json json

Creating Json Output Boto3


You are creating a new dictionary and printing it in each iteration of the loop. You just need the following if you want the bucket names:

# Iterate through each bucketdata = []for bucket in allbuckets['Buckets']:   data.append[{'#BUCKET_NAME': bucket["Name"]}]data = {'data':data}json_str = json.dumps(data)print json_str

If you want to use list comprehension:

data = [{'#BUCKET_NAME': bucket["Name"]} for bucket in allbuckets['Buckets']]data = {'data':data}json_str = json.dumps(data)print json_str


The code is here....after the helloV help:

import boto3import jsonimport strings3client = boto3.client('s3')allbuckets = s3client.list_buckets()data = [{"{#BUCKET_NAME}":bucket["Name"]} for bucket in allbuckets['Buckets']]data = {"data":data}json_pre = json.dumps(data)json_pos = json_pre.translate(None, string.whitespace)print json_pos

The translate to remove whitespaces is necessary to JSON Format.