Check if key is missing after loading json from file in python

"example" in data.keys() will return True or False, so this would be one way to check.

So, given JSON like this...

  { "example": { "title": "example title"}}

And given code to load the file like this...

import json

with open('example.json') as f:

    data = json.load(f)

The following code would return True or False:

x = "example" in data   # x set to True
y = "cheese" in data    # y set to False

You can try:

if data.get("example") == "":
    ...

This will not raise an error, even if the key "example" doesn't exist.

What is happening in your case is that data["example"] does not equal "", and in fact there is no key "example" so you are probably seeing a KeyError which is what happens when you try to access a value in a dict using a key that does not exist. When you use .get("somekey"), if the key "somekey" does not exist, get() will return None and will return the value otherwise. This is important to note because if you do a check like:

if not data.get("example"): 
    ...

this will pass the if test if data["example"] is "" or if the key "example" does not exist.