cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
581
Views
10
Helpful
1
Replies

Converting PYATS output to list format

rasmus.elmholt
Level 7
Level 7

Hi Everyone,

I am trying to use PYATS to pull some LLDP neighbor information from some switches.

The format I get out of it is nested JSON, but I want to have the information in a easy to iterate over list format.

rasmuselmholt_0-1671700734094.png

As you can see above the LLDP information is neste. The best for me would be to have the data in a list contaning the interface neighbors and id.

How do I convert this format to something easy to iterate over like the CDP output from PYATS?

rasmuselmholt_1-1671700888377.png

 

1 Accepted Solution

Accepted Solutions

Marcel Zehnder
Spotlight
Spotlight

Hi

You may use this function:

def genieLldp_2_listOfDicts(lldpdict):
    result = list()
    for localint, remote in lldpdict.get("interfaces").items():
        for remoteint, remotedetails in remote.get("port_id").items():
            for neighbor in remotedetails.get("neighbors").keys():
                result.append({
                    "local_int": localint,
                    "remote_int": remoteint,
                    "neighbor": neighbor.strip()
                })
    return result

Complete example:

import json


def genieLldp_2_listOfDicts(lldpdict):
    result = list()
    for localint, remote in lldpdict.get("interfaces").items():
        for remoteint, remotedetails in remote.get("port_id").items():
            for neighbor in remotedetails.get("neighbors").keys():
                result.append({
                    "local_int": localint,
                    "remote_int": remoteint,
                    "neighbor": neighbor.strip()
                })
    return result



lldp = {
  "interfaces": {
    "GigabitEthernet2": {
      "port_id": {
        "Gi0/3": {
          "neighbors": {
            "IOSvL2.lab          ": {
              "capabilities": [
                "R"
              ],
              "hold_time": 120
            }
          }
        }
      }
    },
    "GigabitEthernet3": {
      "port_id": {
        "Gi0/2": {
          "neighbors": {
            "IOSv.lab            ": {
              "capabilities": [
                "R"
              ],
              "hold_time": 120
            }
          }
        }
      }
    },
    "GigabitEthernet4": {
      "port_id": {
        "Gi0/1": {
          "neighbors": {
            "IOSv2.lab           ": {
              "capabilities": [
                "R"
              ],
              "hold_time": 120
            }
          }
        }
      }
    }
  },
  "total_entries": 3
}

print(json.dumps(genieLldp_2_listOfDicts(lldp), indent=1))

Result:

[
 {
  "local_int": "GigabitEthernet2",
  "remote_int": "Gi0/3",
  "neighbor": "IOSvL2.lab"
 },
 {
  "local_int": "GigabitEthernet3",
  "remote_int": "Gi0/2",
  "neighbor": "IOSv.lab"
 },
 {
  "local_int": "GigabitEthernet4",
  "remote_int": "Gi0/1",
  "neighbor": "IOSv2.lab"
 }
]

HTH

Marcel

View solution in original post

1 Reply 1

Marcel Zehnder
Spotlight
Spotlight

Hi

You may use this function:

def genieLldp_2_listOfDicts(lldpdict):
    result = list()
    for localint, remote in lldpdict.get("interfaces").items():
        for remoteint, remotedetails in remote.get("port_id").items():
            for neighbor in remotedetails.get("neighbors").keys():
                result.append({
                    "local_int": localint,
                    "remote_int": remoteint,
                    "neighbor": neighbor.strip()
                })
    return result

Complete example:

import json


def genieLldp_2_listOfDicts(lldpdict):
    result = list()
    for localint, remote in lldpdict.get("interfaces").items():
        for remoteint, remotedetails in remote.get("port_id").items():
            for neighbor in remotedetails.get("neighbors").keys():
                result.append({
                    "local_int": localint,
                    "remote_int": remoteint,
                    "neighbor": neighbor.strip()
                })
    return result



lldp = {
  "interfaces": {
    "GigabitEthernet2": {
      "port_id": {
        "Gi0/3": {
          "neighbors": {
            "IOSvL2.lab          ": {
              "capabilities": [
                "R"
              ],
              "hold_time": 120
            }
          }
        }
      }
    },
    "GigabitEthernet3": {
      "port_id": {
        "Gi0/2": {
          "neighbors": {
            "IOSv.lab            ": {
              "capabilities": [
                "R"
              ],
              "hold_time": 120
            }
          }
        }
      }
    },
    "GigabitEthernet4": {
      "port_id": {
        "Gi0/1": {
          "neighbors": {
            "IOSv2.lab           ": {
              "capabilities": [
                "R"
              ],
              "hold_time": 120
            }
          }
        }
      }
    }
  },
  "total_entries": 3
}

print(json.dumps(genieLldp_2_listOfDicts(lldp), indent=1))

Result:

[
 {
  "local_int": "GigabitEthernet2",
  "remote_int": "Gi0/3",
  "neighbor": "IOSvL2.lab"
 },
 {
  "local_int": "GigabitEthernet3",
  "remote_int": "Gi0/2",
  "neighbor": "IOSv.lab"
 },
 {
  "local_int": "GigabitEthernet4",
  "remote_int": "Gi0/1",
  "neighbor": "IOSv2.lab"
 }
]

HTH

Marcel

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: