Search code examples
pythonlistpython-re

Is there a way to compare the elements of two lists residing in a json?


I'm currently working on parsing the messages from logs and printing them out. Please see the program below before I explain my problem.

    count = 0
    error_list = ["*SSH connection as gentan (using password) successful.*", "*Deployment State Updated to ATTEMPTING_CONNECTION*"]
    
    for i in data['deploymentStatus']['page']:
        count = count + 1
        regex = re.compile(error_list)
        if re.findall(regex, i['history']) is True:
            string = """
        ========================
            TARGET # %s
        ========================
        [+] IP      => %s
        [+] HISTORY => %s""" % (count, i['address'], i['history'])
            print(string)

Here's the code what i['history'] is equals to.

i['history] = ["13/04/2021 05:42:59:589Z: Attempting connection via ssh as gentan.", "13/04/2021 05:42:59:589Z: Deployment State Updated to ATTEMPTING_CONNECTION"]

Now lastly what I want is, I want the if statement to only pring the logs that match with each other in the error_list and i['history']

The reason why its not matching in my case is because it starts with date and time, is there a way in python to parse that out and compare the string only?

I would greatly appreciate any help. I can also post the complete data['deploymentStatus']['page'] json file but its too long to post here 12k+ lines.


Solution

  • You don't need to use regular expressions. Just use the in operator to test if one of the strings in error_list is in the history string.

    error_list = ["SSH connection as gentan (using password) successful", "Deployment State Updated to ATTEMPTING_CONNECTION"]
    
    for i in data['deploymentStatus']['page']:
        if any(error in i['history'] for error in error_list):
            print("""
            ========================
                TARGET # %s
            ========================
            [+] IP      => %s
            [+] HISTORY => %s""" % (count, i['address'], i['history']))
    
    count = len(data['deploymentStatus']['page']) # no need to calculate this in the loop