DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Check If String Has JSON Inside

08.29.2010
| 6334 views |
  • submit to reddit
        Two very simple methods of checking whether string has JSON inside ...

More accurate one:

module JSON
  def is_json?(string)
    begin
      parse(string).all?
    rescue ParserError
      false
    end
  end
end

Less accurate one (but quicker):

class String
  def is_json?
    self[0..0] == '{'
  end
end