0

I have a JSON string

var str = '{'+
            '"name": "John Doe",'+
            '"company": [{"name": "ABC Corp"}, {"name": "XYZ Corp"}],'+
            '"salary": "$200000"'+
           '}';

I make the ajax call as

$.ajax({
     url: 'url',
     type: 'POST',
     context: document.body,
     dataType: 'json',
     data: str,
     success: function(data){},
     error: function(error){}
     });

How can I escape the double quotes inside the JSON array before making the ajax call.

4
  • 3
    And where exactly are you using this str variable ? Commented Apr 2, 2014 at 19:53
  • escape(str); will definitely escape everything Commented Apr 2, 2014 at 19:53
  • Why would you want to escape those quotes, since they're exactly as they should be? Commented Apr 2, 2014 at 19:57
  • Oh, so now the issue is apparent, you're stringifying a string, and that gives you an error. Just stop stringifying it, and why would you have a string to begin with, $.ajax accepts objects. Commented Apr 2, 2014 at 19:58

2 Answers 2

2

str is already valid JSON (according to JSONLint), so you don't need to escape anything before sending it via $.ajax.

Sign up to request clarification or add additional context in comments.

3 Comments

wait - so for the data option - you can just pass in a string like "test"? I thought it had to be key/value like "key=test"(as string) or {key:test} <-- as object
@ᾠῗᵲᄐᶌ - You can send a string formatted as json to $.ajax. You probably shouldn't but thats a different discussion.
@Jamiec hmm.. never knew that - will have to check out the code
2

It looks like you're going the wrong way - JSON.stringify is used to turn a JSON object to a string, the method you want is JSON.parse.

So change that to:

$.ajax({
     url: 'url',
     type: 'POST',
     context: document.body,
     dataType: 'json',
     data: JSON.parse(str),
     success: function(data){},
     error: function(error){}
     });

You can also pass a string formatted as JSON to $.ajax and as your string was already valid JSON you could just skip that step entirely:

$.ajax({
     url: 'url',
     type: 'POST',
     context: document.body,
     dataType: 'json',
     data: str,
     success: function(data){},
     error: function(error){}
     });

Although you could just build it up as an object to start with if that is easier (It often is than trying to format a string!):

var postData = {
   name:'John Doe'
   ... etc
};

$.ajax({
     url: 'url',
     type: 'POST',
     context: document.body,
     dataType: 'json',
     data: postData,
     success: function(data){},
     error: function(error){}
     });

3 Comments

Why would you parse it if it's a valid string, you can't send an object so jQuery would have to stringify it back again internally ?
@adeneo - Im just answering the question as stated. I was updating with a better solution when you made that comment.
You should note that passing a JSON string will gives a completely different result than passing a JavaScript object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.