Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6319327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:47:18+00:00 2026-05-24T15:47:18+00:00

I have a JSON object and I want to convert it escaping / characters.

  • 0

I have a JSON object and I want to convert it escaping / characters.

Object:

{
 "id":"123",
 "name":"test",
 "link":"https://google.com"
}

Desired result:

{
 "id":"123",
 "name":"test",
 "link":"https:\/\/google.com"
}

How can I do this transformation in Ruby, RoR?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T15:47:18+00:00Added an answer on May 24, 2026 at 3:47 pm

    If it is at all possible, modify the values before they are JSON’d. In activerecord, I believe you can change the value and convert it to JSON – so long as you don’t save the model, that change will be discarded.

    In ruby, JSON is just a string, so you could do

    my_json.gsub('/', '\\/')
    

    This would convert any forward slashes in the keys, too. I don’t know of any reason a JSON string would contain forward slashes outside of a string, so that should be fine.

    If you want to avoid converting the keys, you could use a (slightly complicated) regular expression:

    my_json.gsub(/:\s*"[^"]*\/[^"]*"/) { |m| m.gsub('/', '\\/')  }
    

    This finds a section that starts with a colon, possibly some whitespace after that, then some double quotes. It then looks for some optional stuff (anything that isn’t a double quote), then a forward slash, them more stuff that isn’t a double quote, then an actual double quote. So essentially, the minimum it will find is :"/" – it then passes each matching string into the block, and runs the previous gsub to convert the slashes. The output of the block then replaces whatever was found in the initial gsub.

    I’m sure there are neater ways, so play around.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JSON object holding the following value: @value = {val:test,val1:test1,val2:test2} I want
I have a response object which I want to convert to Json, but somehow
I want to convert the html tag objects to json object in the javascript
I have a JSON String like this $test='{var1:null,var3:null,status:{code:150,message:blah blah}}'; I want to access the
I have an XML file which I want to convert into JSON file using
I can have Spring convert my json POST submission into an object with a
I want to have a JSON object with the value of an attribute as
I have a json object array. I want to search the array and for
I have IQueryable<> object. I want to Convert it into List<> with selected columns
I want to know how can I convert a Java object to JSON by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.