6

I am writing a script to collect some inventory data via REST. I then want to filter it to create a list and two dictionaries which I can use elsewhere in my script.

For example, from this:

{'version': '0.0'
 'response': [{'chassisType': 'C800',
           'family': 'C897VA-K9',
           'hostname': 'chaney-xtr',
           'imageName': 'c800-universalk9-mz.SPA.154-2.T.bin',
           'interfaceCount': '10',
           'lastUpdated': '2014-06-03 01:39:19.855491-07',
           'lineCardId': 'e5bddd56-2194-4b83-8ae5-597893800051',
           'macAddress': '88:5A:92:A4:E7:C8',
           'managementIpAddress': '192.168.2.1',
           'memorySize': '988236K/60339K',
           'networkDeviceId': 'e15789bd-47df-4df9-809f-daf81d15ff2a',
           'numUpdates': 1,
           'platformId': 'C897VA-K9',
           'portRange': 'ATM0, ATM0.1, BRI0, BRI0:1-2, Dialer1, Ethernet0, GigabitEthernet0-8, LISP0, Loopback0-1, NVI0, Virtual-Access1, Virtual-Template1, Vlan1',
           'role': 'Unknown',
           'roleSource': 'auto',
           'serialNumber': 'FGL175124DX',
           'softwareVersion': '15.4(2)T',
           'type': 'UNKNOWN',
           'upTime': '2 weeks, 3 days, 18 hours, 2 minutes',
           'vendor': 'Cisco'},
          {'chassisType': 'C800',
           'family': 'C897VA-K9',
           'hostname': 'chaney-xtr2',
           'imageName': 'c800-universalk9-mz.SPA.154-2.T.bin',
           'interfaceCount': '10',
           'lastUpdated': '2014-06-03 01:39:19.855491-07',
           'lineCardId': 'e5bddd56-2194-4b83-8ae5-597893800051',
           'macAddress': '88:5A:92:A4:E7:C8',
           'managementIpAddress': '192.168.2.2',
           'memorySize': '988236K/60339K',
           'networkDeviceId': 'e15789bd-47df-4df9-809f-daf81d15ff2a',
           'numUpdates': 1,
           'platformId': 'C897VA-K9',
           'portRange': 'ATM0, ATM0.1, BRI0, BRI0:1-2, Dialer1, Ethernet0, GigabitEthernet0-8, LISP0, Loopback0-1, NVI0, Virtual-Access1, Virtual-Template1, Vlan1',
           'role': 'Unknown',
           'roleSource': 'auto',
           'serialNumber': 'XGL175124D3',
           'softwareVersion': '15.4(2)T',
           'type': 'UNKNOWN',
           'upTime': '2 weeks, 3 days, 18 hours, 2 minutes',
           'vendor': 'Cisco'}],
 }

where "platformId" = "C897VA-K9" I want to create a list of IP addresses from managementIpAddress

And two dictionaries using IP address as key

dict1 = {"managementIpAddress": "hostname"}
dict2 = {"managementIpAddress": "platformId"}

How would you go about doing this?

Kind regards,

Ryan

3
  • 4
    What approaches have you tried? Commented Jun 5, 2014 at 3:59
  • Have you tried the python docs? docs.python.org/2/library/json.html Commented Jun 5, 2014 at 4:01
  • As @cwallenpoole mentioned, you should share what you've tried doing so far, and where specifically you got stuck with it. Commented Jun 5, 2014 at 4:07

2 Answers 2

2
from_this = {"version":"0.0","response":[{"macAddress":"88:5A:92:A4:E7:C8","networkDeviceId":"e15789bd-47df-4df9-809f-daf81d15ff2a","lineCardId":"e5bddd56-2194-4b83-8ae5-597893800051","lastUpdated":"2014-06-03 01:39:19.855491-07","platformId":"C897VA-K9","vendor":"Cisco","numUpdates":1,"interfaceCount":"10","portRange":"ATM0, ATM0.1, BRI0, BRI0:1-2, Dialer1, Ethernet0, GigabitEthernet0-8, LISP0, Loopback0-1, NVI0, Virtual-Access1, Virtual-Template1, Vlan1","roleSource":"auto","chassisType":"C800","softwareVersion":"15.4(2)T","upTime":"2 weeks, 3 days, 18 hours, 2 minutes","imageName":"c800-universalk9-mz.SPA.154-2.T.bin","memorySize":"988236K/60339K","managementIpAddress":"192.168.2.1","family":"C897VA-K9","type":"UNKNOWN","serialNumber":"FGL175124DX","role":"Unknown","hostname":"chaney-xtr"}, {"macAddress":"88:5A:92:A4:E7:C8","networkDeviceId":"e15789bd-47df-4df9-809f-daf81d15ff2a","lineCardId":"e5bddd56-2194-4b83-8ae5-597893800051","lastUpdated":"2014-06-03 01:39:19.855491-07","platformId":"C897VA-K9","vendor":"Cisco","numUpdates":1,"interfaceCount":"10","portRange":"ATM0, ATM0.1, BRI0, BRI0:1-2, Dialer1, Ethernet0, GigabitEthernet0-8, LISP0, Loopback0-1, NVI0, Virtual-Access1, Virtual-Template1, Vlan1","roleSource":"auto","chassisType":"C800","softwareVersion":"15.4(2)T","upTime":"2 weeks, 3 days, 18 hours, 2 minutes","imageName":"c800-universalk9-mz.SPA.154-2.T.bin","memorySize":"988236K/60339K","managementIpAddress":"192.168.2.2","family":"C897VA-K9","type":"UNKNOWN","serialNumber":"XGL175124D3","role":"Unknown","hostname":"chaney-xtr2"}]}

dict1 = {}
dict2 = {}

for response_item in from_this['response']:
    dict1[response_item['managementIpAddress']] = response_item['hostname']
    dict2[response_item['managementIpAddress']] = response_item['platformId']

The output looks like this:

In[189]: dict1
Out[187]: {'192.168.2.1': 'chaney-xtr', '192.168.2.2': 'chaney-xtr2'}
In[190]: dict2
Out[188]: {'192.168.2.1': 'C897VA-K9', '192.168.2.2': 'C897VA-K9'}

If this is what you were looking for, let me know if you have any questions.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow! That was fast. Thanks Krishan that's very helpful. For those who asked, I find the Python documentation hard to follow however I did find this answer on this site which was also very helpful: stackoverflow.com/questions/23024477/….
0

you can parse the json data,then add some if and else case to judge whether data is needed;then use eval statement to gengerate a dict

1 Comment

Your answer will be clearer if you expand on it and supply example code

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.