You can create a method to do the checking:
def valid_json?(json) JSON.parse(json) return true rescue JSON::ParserError => e return falseend
You can parse it this way
begin JSON.parse(string) rescue JSON::ParserError => e # do smthend # or for method get_parsed_responsedef get_parsed_response(response) parsed_response = JSON.parse(response)rescue JSON::ParserError => e # do smthend
I think parse_json should return nil if it's invalid and shouldn't error out.
parse_json
nil
def parse_json string JSON.parse(string) rescue nilendunless json = parse_json string parse_a_different_wayend