How to remove backslashes in string generated through .to_json function. Rails 3.2

json, ruby-on-rails

Solution

You can just `gsub!` to replace the `\"` with a single quote `'`, like so:

a.gsub!(/\"/, '\'')

Problem

``` ["{\"id\":317277848652099585,\"Tweet text\":\"Food Carnival by KMS Homemade Food\\nOn the occasion of this Holi and Good Friday, KMS Homemade Foods invites you... http://t.co/2Y2mO6vr76\",\"Word count\":21,\"Url\":\"true\"}"] ``` a is a hash with some keys and values. ``` a = a.to_json ``` converts the hash to a string. Now a is a string with all backslashes... I know tha ``` puts a ``` returns a string with all backslashes removed but what if i want to store the 'backslash removed string' in a variable?

Original source