check if a json file already has the data i am overriding in python
Question: I am currently making a python script that automates a task by sending an
email after first parsing the data from a website then sending the message
from that data using twilio.

But what I want is to first compare the data parsed with the already existing
json file that I parsed previously and if it has same date or message then it
should not send the message.

I have no idea how to do this I have tried to load the json file but I
couldn't get it to work properly.

Here is my json file that I want to check:

    
    
    {
        "date": "11/02/2016 11:42:57", 
        "message": "Dear students,\r\n\r\nAs informed in the class, this is to remind you Today special class from 6 to 6.50 pm at same venue SJT 126.\r\n\r\nregards\r\n\r\nR. Raghavan\r\nSITE", 
        "name": "RAGHAVAN R (SITE)", 
        "subject": "ITE308 - Distributed Systems - TH"
    }
    

here is my code:

    
    
    infoTable = tables[0].findAll('tr')
        name = infoTable[2].findAll('td')[0].text
        if (len(name) is 0):
            return None
        subject = infoTable[2].findAll('td')[1].text
        msg = infoTable[2].findAll('td')[2].text
        sent = infoTable[2].findAll('td')[3].text
    textmyself.textmyself(msg)
        # Parsing the open hours of the faculties
        outputPath = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'output')
        if os.path.isdir(outputPath) is False:
            os.makedirs(outputPath)
        result = {'name': name, 'subject': subject, 'message': msg, 'date': sent}
        with open('output/' + str(facultyID) + '.json', 'w') as outfile:
            json.dump(result, outfile, indent=4)
        return result
    

**Update:** Here is what I tried and found working but json file should
already be there if one is running script for the first time, so is my code
correct?

    
    
    with open('output/WS.json') as data_file:
            data = json.load(data_file)
        if data["date"] == sent:
            outputpath = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'output')
            if os.path.isdir(outputpath) is False:
             os.makedirs(outputpath)
            result = {'name': name, 'subject': subject, 'message': msg, 'date': sent}
            with open('output/' + str(facultyID) + '.json', 'w') as outfile:
              json.dump(result, outfile, indent=4)
            return result
        else:
            outputpath = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'output')
            if os.path.isdir(outputpath) is False:
             os.makedirs(outputpath)
            result = {'name': name, 'subject': subject, 'message': msg, 'date': sent}
            with open('output/' + str(facultyID) + '.json', 'w') as outfile:
              json.dump(result, outfile, indent=4)
            textmyself.textmyself(msg)
            return result
    


Answer: You can load the old JSON file right away by using:

    
    
    import json
    with open('old_data.json') as f:
        old_message = json.load(f)
    

Then you can compare

    
    
    if not old_message['date'] == sent:
        # send your mail etc.
    

Then bundle your data in another JSON and write the file back with the new
message:

    
    
        new_message = {
            "date" : sent,
            ... }
        with open('old_data.json', 'w') as f:
            json.dump(new_message, f)
    

Putting together the pieces with what you already have, and also adding some
error handling, should solve your issue.

