Leonardo Gialluisi
Leonardo Gialluisi

Reputation: 69

Creating Json Output Boto3

I have the code below:

import boto3
import datetime
import json

now = datetime.datetime.now()

cw = boto3.client('cloudwatch', region_name='sa-east-1')
s3client = boto3.client('s3')

# Get a list of all buckets
allbuckets = s3client.list_buckets()

# Iterate through each bucket
for bucket in allbuckets['Buckets']:
    # For each bucket item, look up the cooresponding metrics from CloudWatch
    response = cw.get_metric_statistics(Namespace='AWS/S3',
                                        MetricName='BucketSizeBytes',
                                        Dimensions=[
                                            {'Name': 'BucketName', 'Value': bucket['Name']},
                                            {'Name': 'StorageType', 'Value': 'StandardStorage'},
                                        ],
                                        Statistics=['Average'],
                                        Period=3600,
                                        StartTime=(now-datetime.timedelta(days=2)).isoformat(),
                                        EndTime=now.isoformat()
                                        )
    for item in response["Datapoints"]:
            data = {}
            data['data'] = []
            data['data'].append ({'#BUCKET_NAME': bucket["Name"]})
            json_str = json.dumps(data)
            print json_str

The output is:

{"data": [{"#BUCKET_NAME": "bucket1"}]}
{"data": [{"#BUCKET_NAME": "bucket2"}]}
{"data": [{"#BUCKET_NAME": "bucket3"}]}
{"data": [{"#BUCKET_NAME": "bucket4"}]}

But i need the output in the following format:

    {"data":[{"{#BUCKET_NAME}":"bucket1"},{"{#BUCKET_NAME}":"bucket2"},
             {"{#BUCKET_NAME}":"bucket3"},{"{#BUCKET_NAME}":"bucket4"}]}

Where i am doing wrong ?

Upvotes: 2

Views: 11557

Answers (2)

Leonardo Gialluisi
Leonardo Gialluisi

Reputation: 69

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

import boto3
import json
import string

s3client = 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.

Upvotes: 1

helloV
helloV

Reputation: 52433

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 bucket
data = []
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

Upvotes: 2

Related Questions