Solution using the Pandas module:
import pandas as pd def get_ip(dct): if isinstance(dct, dict): if 'type' in dct and 'ip' in dct and 'dns' in dct and dct['type'] == '2': return {dct['dns']: {'ip':dct['ip']}} return pd.np.nan In [113]: (pd.DataFrame(a['hosts']) ...: .interfaces ...: .apply(pd.Series) ...: .applymap(get_ip) ...: .apply(lambda x: x[x.first_valid_index()], axis=1) ...: .tolist() ...: ) ...: Out[113]: [{'my.hostname.local': {'ip': '1.1.1.1'}}, {'my01.hostname.local': {'ip': '1.1.1.1'}}, {'my13.hostname.local': {'ip': '1.1.1.1'}}]
PS I used dns from interfaces as the hostname
Setup:
a = { 'hosts': [{u'available': u'0', u'disable_until': u'0', u'error': u'', u'errors_from': u'0', u'groups': [{u'groupid': u'361'}], u'host': u'my.hostname.local', u'hostid': u'144844', u'interfaces': {u'302972': {u'dns': u'my.hostname.local', u'hostid': u'144844', u'interfaceid': u'302972', u'ip': u'1.1.1.1', u'main': u'1', u'port': u'161', u'type': u'2', u'useip': u'1'}}, u'ipmi_authtype': u'-1', u'ipmi_available': u'0', u'ipmi_disable_until': u'0', u'ipmi_error': u'', u'ipmi_errors_from': u'0', u'ipmi_password': u'', u'ipmi_privilege': u'2', u'ipmi_username': u'', u'jmx_available': u'0', u'jmx_disable_until': u'0', u'jmx_error': u'', u'jmx_errors_from': u'0', u'lastaccess': u'0', u'maintenance_from': u'0', u'maintenance_status': u'0', u'maintenance_type': u'0', u'maintenanceid': u'0', u'maintenances': [], u'proxy_hostid': u'11223', u'snmp_available': u'2', u'snmp_disable_until': u'0', u'snmp_errors_from': u'0', u'status': u'0'}, {u'available': u'0', u'disable_until': u'0', u'error': u'', u'errors_from': u'0', u'groups': [{u'groupid': u'361'}], u'host': u'my2.hostname.local', u'hostid': u'144844', u'interfaces': {u'302973': {u'dns': u'my01.hostname.local', u'hostid': u'144844', u'interfaceid': u'302973', u'ip': u'1.1.1.1', u'main': u'1', u'port': u'161', u'type': u'2', u'useip': u'1'}, u'321123': {u'dns': u'my02.hostname.local', u'hostid': u'321', u'interfaceid': u'321312', u'ip': u'2.2.2.2', u'type': u'3', u'useip': u'1'}, u'675675': {u'dns': u'my03.hostname.local', u'hostid': u'566454', u'interfaceid': u'8987798', u'ip': u'3.3.3.3', u'type': u'1', u'useip': u'1'}}, u'ipmi_authtype': u'-1', u'ipmi_available': u'0', u'ipmi_disable_until': u'0', u'ipmi_error': u'', u'ipmi_errors_from': u'0', u'ipmi_password': u'', u'ipmi_privilege': u'2', u'ipmi_username': u'', u'jmx_available': u'0', u'jmx_disable_until': u'0', u'jmx_error': u'', u'jmx_errors_from': u'0', u'lastaccess': u'0', u'maintenance_from': u'0', u'maintenance_status': u'0', u'maintenance_type': u'0', u'maintenanceid': u'0', u'maintenances': [], u'proxy_hostid': u'11223', u'snmp_available': u'2', u'snmp_disable_until': u'0', u'snmp_errors_from': u'0', u'status': u'0'}, {u'available': u'0', u'disable_until': u'0', u'error': u'', u'errors_from': u'0', u'groups': [{u'groupid': u'361'}], u'host': u'my3.hostname.local', u'hostid': u'144844', u'interfaces': {u'402972': {u'dns': u'my13.hostname.local', u'hostid': u'144844', u'interfaceid': u'402972', u'ip': u'1.1.1.1', u'main': u'1', u'port': u'161', u'type': u'2', u'useip': u'1'}, u'421123': {u'dns': u'my12.hostname.local', u'hostid': u'321', u'interfaceid': u'421312', u'ip': u'2.2.2.2', u'type': u'3', u'useip': u'1'}, u'475675': {u'dns': u'my13.hostname.local', u'hostid': u'566454', u'interfaceid': u'8987798', u'ip': u'3.3.3.3', u'type': u'1', u'useip': u'1'}}, u'ipmi_authtype': u'-1', u'ipmi_available': u'0', u'ipmi_disable_until': u'0', u'ipmi_error': u'', u'ipmi_errors_from': u'0', u'ipmi_password': u'', u'ipmi_privilege': u'2', u'ipmi_username': u'', u'jmx_available': u'0', u'jmx_disable_until': u'0', u'jmx_error': u'', u'jmx_errors_from': u'0', u'lastaccess': u'0', u'maintenance_from': u'0', u'maintenance_status': u'0', u'maintenance_type': u'0', u'maintenanceid': u'0', u'maintenances': [], u'proxy_hostid': u'11223', u'snmp_available': u'2', u'snmp_disable_until': u'0', u'snmp_errors_from': u'0', u'status': u'0'}] }
forI can not solve the problem as I thought before. The search process is trivial:a.find('ip')function that would output allipkeys with their value and, possibly, with the “path” by which they can be found. It's elementary why nobody has done this yet? The fact that in the end I need to get I wrote above, but I dare to believe that in the future I will need to look for something else in this dictionary. - user231551host- always one or there may be many? Areinterfacesalways listed as a dictionary or can they be like a list? - MaxUhostsalways one. It always contains onelistconsisting of a set of dictionaries, in each of which there can be only one key ofinterfacesbut there can be a set ofintkeys whose values ​​can differ. Sorry if it's too hard to explain. - user231551