ihue ihue - 1 year ago 138
Python Question

Access JSON Key in Python

I'm trying to print out all the

fields of my JSON data.

I have

# Last updated : BH | 8/31/2016

import requests
import json

ssc_ip = raw_input("What is your SSC Host (Ex. ) ? : ")
if not ssc_ip:
ssc_ip = ''

cpe_num = raw_input("How many CPE(s) you want to delete ? : ")
print '\n'

url = ssc_ip+'vse/vcpes'
json_data = requests.get(url).json()
# print json_data

for x in json_data:
print json_data.cpe_mac

I kept getting

AttributeError: 'dict' object has no attribute 'cpe_mac'


print json_data['cpe_mac']

I got

KeyError: 'cpe_mac'

Answer Source

You have a nested dictionary, it should be:

for x in json_data['data']:
    print x['cpe_mac']
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download