Parse JSON Safely

Parse JSON with a fallback value returned if parsing fails.

Code

Utilities
require 'json'
begin
  puts JSON.parse(jsonStr).to_json
rescue
  puts JSON.parse(fallback).to_json
end

Parameters

JSON string to parse.

Fallback value if parsing fails (as JSON).

Server

More Ruby Snippets