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 6245497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:31:00+00:00 2026-05-24T12:31:00+00:00

I have a textbox that posts info to the server and it’s in JSON

  • 0

I have a textbox that posts info to the server and it’s in JSON format. Lets say I want to enter two quotes for the value, and the JSON struct would look like:

{
    "test": """"
}

I need it to look like:

{
    "test": "\"\""
}

so it will follow JSON standards and can be parsable/stringifyable.

I tried using

 var val = myVal.replace('"', "\\\"");

but this didn’t work. val ends up with only one escaped quote like so \""Any help is much appreciated!

  • 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-24T12:31:01+00:00Added an answer on May 24, 2026 at 12:31 pm

    My answer makes some assumptions, as I’ve had to fill in the rather sizeable gaps in your question:

    • The user will enter a text string into a textbox;
    • Your script will read the textbox contents, and use those contents as the value of one of the items in a JSON string that it’s building;
    • The script sends this resulting JSON string to the server somehow.

    If I’ve got that right, let’s proceed…


    Baseline code

    So, with some placeholders, you’re doing:

    function get_contents_of_textbox() {
       // Dummy data for example
       return 'My mum pushed and I said "Hello World"!';
    }
    
    function send_to_server(json_str) {
       // Dummy action: display JSON string
       console.log(json_str);
    }
    
    var myVal = get_contents_of_textbox();
    var JSON  = '{ "test": "' + myVal + '" }';
    send_to_server(JSON);
    

    Live demo, showing the malformed JSON.


    Initial attempt

    To ensure that JSON is valid, escape any quotes and backslashes that it may contain. You already gave it a go:

    myVal = myVal.replace('"', "\\\"");
    

    and the result of your attempt is:

    { "test": "My mum pushed and I said \"Hello World"!" }
    

    Only the first quote has been escaped. This is because only one instance of the search string is replaced by default.

    The Mozilla documentation says:

    To perform a global search and replace, either include the g flag in
    the regular expression or if the first parameter is a string, include
    g in the flags parameter.


    Working attempt

    Unfortunately, the flags parameter is non-standard, so let’s switch to the regex version of replace, and use the /g switch in it:

    myVal = myVal.replace(/"/g, '\\"');
    

    (You’ll notice that I also condensed the replacement string, for brevity.)

    Result:

    { "test": "My mum pushed and I said \"Hello World\"!" }
    

    Live demo. Hurrah!


    Complete solution

    Let’s also add logic to escape backslashes, and we end up with this:

    function get_contents_of_textbox() {
       // Dummy data for example
       return 'My mum pushed [a back\\slash] and I said "Hello World"!';
    }
    
    function send_to_server(json_str) {
       // Dummy action: display JSON string
       console.log(json_str);
    }
    
    var myVal = get_contents_of_textbox();
    myVal = myVal.replace(/\\/g, '\\\\'); // escape backslashes
    myVal = myVal.replace(/"/g, '\\"');   // escape quotes
    
    var JSON  = '{ "test": "' + myVal + '" }';
    send_to_server(JSON);
    

    Result:

    { "test": "My mum pushed [a back\\slash] and I said \"Hello World\"!" }
    

    Live demo.

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

Sidebar

Related Questions

I have a textbox that posts data with js keyboard enter event. Is there
I would like to have a TextBox that supports AutoComplete and lets users type
I have an ASP.NET page that has two input elements: A TextBox that is
I have an asp:button with an onclick property that posts back to the server.
I have a TextBox that is bound to a Text-property on an Entity-object. I'd
I have a textbox that I would like for only numbers. But if I
I have a textbox that i am binding to the viewmodel's string property. The
I have a textbox that when you put focus into it, it drops down
I have a TextBox that has the TextChanged event set declaratively. In some cases,
I've recently added the JQuery autocomplete plug in and have a textbox that autocompletes

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.