Q:

AttributeError: 'unicode' object has no attribute 'Bytes_Written'

I am trying to query a server the code is really simple but I don’t understand why I get this error. I am querying bytes read bytes written and IO operation because I want to later show them in a line chart.
Data
{
  "Number of Devices": 2,
  "Block Devices": {
    "bdev0": {
      "Backend_Device_Path": "/dev/disk/by-path/ip-192.168.26.1:3260-iscsi-iqn.2010-10.org.openstack:volume-d1c8e7c6-8c77-444c-9a93-8b56fa1e37f2-lun-010.0.0.142",
      "Capacity": "2147483648",
      "Guest_Device_Name": "vdb",
      "IO_Operations": "97069",
      "Bytes_Written": "34410496",
      "Bytes_Read": "363172864"
    },
    "bdev1": {
      "Backend_Device_Path": "/dev/disk/by-path/ip-192.168.26.1:3260-iscsi-iqn.2010-10.org.openstack:volume-b27110f9-41ba-4bc6-b97c-b5dde23af1f9-lun-010.0.0.146",
      "Capacity": "2147483648",
      "Guest_Device_Name": "vdb",
      "IO_Operations": "93",
      "Bytes_Written": "0",
      "Bytes_Read": "380928"
    }
  }
}

code
#Get data checkpoint size
url = 'url'
r = requests.get(url)
data = r.text
print data

def counterVolume(data):
  for each in data:
        x = each.Bytes_Written
        y = each.Bytes_Read
        z = each.IO_Operations
        print {'Bytes_written': x, 'Bytes_Read': y, 'IO_Operations': z}

I want the output to be 
{'Bytes_written': value, 'Bytes_Read': value, 'IO_Operations': value}

A:

Assuming you've got a valid JSON response, retrieve your data as such:
data = r.json()

Then iterate over the block devices:
for devname, stats in data['Block Devices'].iteritems():
    print '{} had {Bytes_Read} read and {Bytes_Written} written'.format(devname, **stats)

You can print as JSON a subset of the response using:
import json

wanted = {'Bytes_Written', 'Bytes_Written', 'IO_Operation'}
for d in data['Block Devices'].itervalues():
    values = {k: v for k, v in d.iteritems() if k in wanted}
    print json.dumps(values)

