How to parse complex json in python 2.7.5? -


i trying list names of puppet classes puppet enterprise 3.7 puppet master, using puppet's rest api.

here script:

#!/usr/bin/env python  import requests import json  url='https://ppt-001.example.com:4433/classifier-api/v1/groups' headers = {"content-type": "application/json"} data={} cacert='/etc/puppetlabs/puppet/ssl/certs/ca.pem' key='/etc/puppetlabs/puppet/ssl/private_keys/ppt-001.example.com.pem' cert='/etc/puppetlabs/puppet/ssl/certs/ppt-001.example.com.pem' result = requests.get(url,         data=data, #no data needed request         headers=headers, #dict {"content-type":"application/json"}         cert=(cert,key), #key/cert pair          verify=cacert         ) print json.dumps( result.json(), sort_keys=true, indent=4, separators=(',', ': '))  in result.json:     print 

here error message when execute script:

traceback (most recent call last):   file "./add-group.py", line 42, in <module>     in result.json: typeerror: 'instancemethod' object not iterable 

here sample of data rest api:

[     {         "classes": {},         "environment": "production",         "environment_trumps": false,         "id": "00000000-0000-4000-8000-000000000000",         "name": "default",         "parent": "00000000-0000-4000-8000-000000000000",         "rule": [             "and",             [                 "~",                 "name",                 ".*"             ]         ],         "variables": {}     },     {         "classes": {             "puppet_enterprise": {                 "certificate_authority_host": "ppt-001.example.com",                 "console_host": "ppt-001.example.com",                 "console_port": "443",                 "database_host": "ppt-001.example.com",                 "database_port": "5432",                 "database_ssl": true,                 "mcollective_middleware_hosts": [                     "ppt-001.example.com"                 ],                 "puppet_master_host": "ppt-001.example.com",                 "puppetdb_database_name": "pe-puppetdb",                 "puppetdb_database_user": "pe-puppetdb",                 "puppetdb_host": "ppt-001.example.com",                 "puppetdb_port": "8081"             }         },         "environment": "production",         "environment_trumps": false,         "id": "52c479fe-3278-4197-91ea-9127ba12474e",         "name": "pe infrastructure",         "parent": "00000000-0000-4000-8000-000000000000",         "variables": {}     }, . . . 

how should go access name key , getting values default , pe infrastructure?

i have read other answers here on saying 1 should use json.loads() , have tried using parsed_json = json.loads(result.json()) results in error message:

traceback (most recent call last):   file "./add-group.py", line 38, in <module>     parsed_json = json.loads(result.json())   file "/usr/lib64/python2.7/json/__init__.py", line 338, in loads     return _default_decoder.decode(s)   file "/usr/lib64/python2.7/json/decoder.py", line 365, in decode     obj, end = self.raw_decode(s, idx=_w(s, 0).end()) typeerror: expected string or buffer 

print json.dumps( result.json(), sort_keys=true, indent=4, separators=(',', ': ')) first parameter of json.dumps must string or buffer, stated typeerror getting (typeerror: expected string or buffer).

your variable result instance of response, , method .json() return dictionary. since you're passing result of .json() json.dumps(), you're getting error. either use result.json() dictionary corresponding response, or change json.dumps line print json.dumps( result.text, sort_keys=true, indent=4, separators=(',', ': ')) result.text json result string/unicode.

after change, access name attribute, like:

for item in r.json():     try:         print item['name']     expect keyerror:         print "there no 'name' attribute" 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -