You can use compile
docs and eval
docs:
def something(s1, s2, op="=="):
# if using an older python version which doesn't supports f-string
# use something like `format` to put the `op` variable in the string.
if eval(compile(f"bool(s1 {op} s2)", "<string>", "eval")):
print("Success")
else:
print("Error")
s1 = "Search_string"
s2 = "searchstring"
op = "==" # load the actual string from your json, this is just for demo
something(s1, s2, op) # prints Error
something(s1, s2, "!=") # prints Success
something(s1, s1, op) # prints Success
Load the operator string from your json file, I hope you know how to do that.
For more info on eval
and compile
see this SO answer, very thorough, no point of me trying to explain them here.
Warning: Beware code like this can be dangerous if you don't have control over whats coming from the json file. Someone can insert any random code in your python script just by editing the json file.