Home > database >  Python parsing JSON data in a better way
Python parsing JSON data in a better way

Time:06-09

I have some JSON data I need to parse. But cant get it fully right and the code doesn't look good.

My first question is, can this be written in a better way than using nested for loops?

Second question. Based on how the JSON structure is setup I cant get a reliable output. I would like to get below output. But because inet.0 and inet.6 is in same level don't get both. Is there a neat way of getting this?

('192.168.0.1', 'Established', 'test-x1', '11996', '463')
('192.168.1.2', 'down', 'test-x2', '31996', '363')
#!/usr/bin/python3
import json
json_input = """
{
    "bgp-information" : [
    {
        "attributes" : {"xmlns" : "http://xml.juniper.net/junos/0/junos-routing"}, 
        "bgp-thread-mode" : [
        {
            "data" : "BGP I/O"
        }
        ], 
        "thread-state" : [
        {
        }
        ], 
        "group-count" : [
        {
            "data" : "23"
        }
        ], 
        "peer-count" : [
        {
            "data" : "31"
        }
        ], 
        "down-peer-count" : [
        {
            "data" : "2"
        }
        ], 
        "bgp-peer" : [
        {
            "attributes" : {"junos:style" : "terse", 
                            "heading" : "Peer                     AS      InPkt     OutPkt    OutQ   Flaps Last Up/Dwn State|#Active/Received/Accepted/Damped..."
                           }, 
            "peer-address" : [
            {
                "data" : "192.168.0.1"
            }
            ], 
            "peer-as" : [
            {
                "data" : "65000"
            }
            ], 
            "input-messages" : [
            {
                "data" : "550950"
            }
            ], 
            "output-messages" : [
            {
                "data" : "41235"
            }
            ], 
            "route-queue-count" : [
            {
                "data" : "0"
            }
            ], 
            "flap-count" : [
            {
                "data" : "4"
            }
            ], 
            "elapsed-time" : [
            {
                "data" : "19w0d 23:55:24", 
                "attributes" : {"junos:seconds" : "11577324"}
            }
            ], 
            "description" : [
            {
                "data" : "test-x1"
            }
            ], 
            "peer-state" : [
            {
                "data" : "Established", 
                "attributes" : {"junos:format" : "Establ"}
            }
            ], 
            "bgp-rib" : [
            {
                "attributes" : {"junos:style" : "terse"}, 
                "name" : [
                {
                    "data" : "inet.0"
                }
                ], 
                "active-prefix-count" : [
                {
                    "data" : "9292"
                }
                ], 
                "received-prefix-count" : [
                {
                    "data" : "11929"
                }
                ], 
                "accepted-prefix-count" : [
                {
                    "data" : "11996"
                }
                ], 
                "suppressed-prefix-count" : [
                {
                    "data" : "0"
                }
                ]
            }, 
            {
                "attributes" : {"junos:style" : "terse"}, 
                "name" : [
                {
                    "data" : "inet6.0"
                }
                ], 
                "active-prefix-count" : [
                {
                    "data" : "48"
                }
                ], 
                "received-prefix-count" : [
                {
                    "data" : "493"
                }
                ], 
                "accepted-prefix-count" : [
                {
                    "data" : "463"
                }
                ], 
                "suppressed-prefix-count" : [
                {
                    "data" : "0"
                }
                ]
            }
            ]
        },
        {
            "attributes" : {"junos:style" : "terse"}, 
            "peer-address" : [
            {
                "data" : "192.168.1.2"
            }
            ], 
            "peer-as" : [
            {
                "data" : "65001"
            }
            ], 
            "input-messages" : [
            {
                "data" : "679978"
            }
            ], 
            "output-messages" : [
            {
                "data" : "43663"
            }
            ], 
            "route-queue-count" : [
            {
                "data" : "0"
            }
            ], 
            "flap-count" : [
            {
                "data" : "2"
            }
            ], 
            "elapsed-time" : [
            {
                "data" : "20w1d 20:40:58", 
                "attributes" : {"junos:seconds" : "1256858"}
            }
            ], 
            "description" : [
            {
                "data" : "test-x2"
            }
            ], 
            "peer-state" : [
            {
                "data" : "down", 
                "attributes" : {"junos:format" : "Establ"}
            }
            ], 
            "bgp-rib" : [
            {
                "attributes" : {"junos:style" : "terse"}, 
                "name" : [
                {
                    "data" : "inet.0"
                }
                ], 
                "active-prefix-count" : [
                {
                    "data" : "9953"
                }
                ], 
                "received-prefix-count" : [
                {
                    "data" : "31996"
                }
                ], 
                "accepted-prefix-count" : [
                {
                    "data" : "31996"
                }
                ], 
                "suppressed-prefix-count" : [
                {
                    "data" : "0"
                }
                ]
            }, 
            {
                "attributes" : {"junos:style" : "terse"}, 
                "name" : [
                {
                    "data" : "inet6.0"
                }
                ], 
                "active-prefix-count" : [
                {
                    "data" : "98"
                }
                ], 
                "received-prefix-count" : [
                {
                    "data" : "463"
                }
                ], 
                "accepted-prefix-count" : [
                {
                    "data" : "363"
                }
                ], 
                "suppressed-prefix-count" : [
                {
                    "data" : "0"
                }
                ]
            }
            ]
        }
        ]
    }
    ]
}
"""
json_out = json.loads(json_input)
for value1 in json_out['bgp-information']:
    for value2 in value1['bgp-peer']:
        for value in value2['peer-address']:
            peer = (value['data'])
        for value in value2['peer-state']:
            state = (value['data'])
        for value in value2['description']:
            desc = (value['data'])        
        for value3 in value2['bgp-rib']: # Below part is what i need help with. Assign first hit to variable inet0_accepted_prefix_count and second to inet6_accepted_prefix_count                   
            for value in value3['accepted-prefix-count']:     
                inet0_accepted_prefix_count = (value['data'])
            for value in value3['accepted-prefix-count']: 
                inet6_accepted_prefix_count = (value['data'])       
            print(inet0_accepted_prefix_count,inet6_accepted_prefix_count)
        combined = (peer,state,desc,inet0_accepted_prefix_count,inet6_accepted_prefix_count) #inet0_accepted_prefix_count is missing
        print(combined)

CodePudding user response:

You need to collect the names: "inet.0" and "inet6.0" as entries in a separate dict to be able to collect the associated "accepted-prefix-count" values:

# json string elided
json_out = json.loads(json_input)
for value1 in json_out['bgp-information']:
    for value2 in value1['bgp-peer']:
        for value in value2['peer-address']:
            peer = (value['data'])
        for value in value2['peer-state']:
            state = (value['data'])
        for value in value2['description']:
            desc = (value['data'])
        d = {}
        for value3 in value2['bgp-rib']:                   
            for value in value3['name']:     
                name = value['data']
            for value in value3['accepted-prefix-count']: 
                accepted_prefix_count = value['data']    
            d[name] = accepted_prefix_count
        combined = (peer, state, desc, d['inet.0'], d['inet6.0']) 
        print(combined)

Output as requested

CodePudding user response:

this code should work for your needs. You could improve/shorten the structure of the json data by taking out the "data" name-space, but only if you know that you won't append anything to the catagory

For exam. - "description":[{"data": "test-x2"}] turn into -> "description": "test-x2"

but this code should work with your current json:

json_out = json.loads(json_input)
for value in json_out["bgp-information"][0]["bgp-peer"]:
    peer = value["peer-address"][0]["data"]
    state = value["peer-state"][0]["data"]
    desc = value["description"][0]["data"]
    inet0_accepted_prefix_count = value["bgp-rib"][0]["accepted-prefix-count"][0]["data"]
    inet6_accepted_prefix_count = value["bgp-rib"][1]["accepted-prefix-count"][0]["data"]

    combined = (peer,state,desc,inet0_accepted_prefix_count,inet6_accepted_prefix_count)
    print(combined)

CodePudding user response:

Juniper JSON output is not in a super easy format comparing to some other vendors so I needed to bring in some exceptions. If someone needs to parse Junos BGP output here's an example based on @MarcusDevs answer:

json_out = json.loads(json_input)   
for value in json_out["bgp-information"][0]["bgp-peer"]:
            peer = value["peer-address"][0]["data"]
            state = value["peer-state"][0]["data"]
            try:
                desc = value["description"][0]["data"]
            except:
                desc = "X"
            try:
                for value1 in value["bgp-rib"]:
                    try:
                        name = value1["name"][0]["data"]
                    except:
                        name = "X" 
                    try:
                        accepted_prefix_count = value1["accepted-prefix-count"][0]["data"]
                    except:
                        accepted_prefix_count = "X"
                    combined = (peer,state,accepted_prefix_count,name,desc)
                    print(combined)
            except:
                name = "X" 
                accepted_prefix_count = "X"
                combined = (peer,state,accepted_prefix_count,name,desc)
                print(combined)
  • Related