I have a string in Python, I want to know if it is valid JSON.
json.loads(mystring)
will raise an error if the string is not JSON but I don\'t want to
To verify the string would require parsing it - so if you checked then converted it would literally take twice as long. Catching the exception is the best way. Interestingly, you can still use an if-else style expression:
try:
json_object = json.loads(json_string)
except ValueError, e:
pass # invalid json
else:
pass # valid json