Search code examples
pythonarraysjsonfilterconditional-statements

Filtering JSON array based on conditional value in python


I am struggling to filter a huge JSON file. The data are presented as:

{
    'network': '2a0d:4bc0::/29',
    'metric': 100,
    'primary': true,
    'id':'2a0d: 4bc0::/29',
    'bgp':{
        'origin': 'IGP',
        'ext_communities': [],
        'large_communities': [
            [6695, 1000, 1],
            [6695, 1902, 9009]
        ],
        'med': 0,
        'as_path': [34549, 3214],
        'next_hop': '2001:7f8:a0::86f5:0:1fe80::42de:ad03:61d1:efe4',
        'communities':[
            [0, 6939],
            [0, 9009],
            [0, 32934],
            [0, 48793],
            [0, 57344],
            [3214, 200],
            [6695, 9145],
            [34549, 100],
            [34549, 300],
            [65212, 11000],
            [65101, 1001],
            [65102, 1000],
            [65103, 276],
            [65104, 150]
        ],
        'local_pref': 100
    }
}
>>> with gzip.open(file, 'rt') as f:
  ....data = json.load(f)

I am able to access data by coding as follow using the different keys:

>>> for i in data: 
   ...for element in data[i][exported]:
   .....print(element['bgp']['communities'], element['bgp']['large_communities'])

where I have the following output:

[
    [0, 12989],
    [0, 13335],
    [0, 15133],
    [0, 15169],
    [0, 16509],
    [0, 20940],
    [0, 22822],
    [0, 2906],
    [0, 32590],
    [0, 48641],
    [0, 49029],
    [0, 714],
    [65101, 11077],
    [65102, 11000],
    [65103, 724],
    [65104, 150]
]

What I want is to filter value which initiated by '0' for example [0, 9009], [0, 48793].... where the output could be look like:

[
    [0, 12989],
    [0, 13335],
    [0, 15133],
    [0, 15169],
    [0, 16509],
    [0, 20940],
    [0, 22822],
    [0, 2906],
    [0, 32590],
    [0, 48641],
    [0, 49029],
    [0, 714]
]

or those which begin by 65101

I don't know which filter methods to use.


Solution

  • Try this

    # Load your data
    data = None
    with open(file, 'r') as file:
        data = json.loads(file.read())
    
    # Load communities field, filter on values with 0 or 65101 in the first column
    communities = data['bgp']['communities']
    result = list(filter(lambda x: x[0] == 0 or x[0] == 65101, communities))