Added better error handling when dealing with json.