42

I use JQuery to get Json data, but the data it display has double quotes. It there a function to remove it?

$('div#ListingData').text(JSON.stringify(data.data.items[0].links[1].caption))

it returns:

"House"

How can I remove the double quote? Cheers.

1
  • 1
    javascript replace function should work May 16, 2013 at 22:24

9 Answers 9

69

Use replace:

var test = "\"House\"";
console.log(test);
console.log(test.replace(/\"/g, ""));  

// "House"
// House

Note the g on the end means "global" (replace all).

3
  • 1
    Awesome! This what exactly what I was looking for. I replace the double quotes, and try with some characters. Nice! thanks!!
    – AlvaroAV
    Nov 15, 2013 at 9:40
  • 2
    Regex, although it works, can cause a lot of memory waste. I think that the user should either just not use JSON.stringify() or if it is a JSON response that is just a sentence, use JSON.parse(). Those regexes add up when things scale. Just saying. Oct 23, 2015 at 13:40
  • 3
    Does not trim starting/ending quotes. It strips all quotes.
    – Yannis
    Oct 23, 2017 at 15:10
25

For niche needs when you know your data like your example ... this works :

JSON.parse(this_is_double_quoted);

JSON.parse("House");  // for example
8

The stringfy method is not for parsing JSON, it's for turning an object into a JSON string.

The JSON is parsed by jQuery when you load it, you don't need to parse the data to use it. Just use the string in the data:

$('div#ListingData').text(data.data.items[0].links[1].caption);
7

Someone here suggested using eval() to remove the quotes from a string. Don't do that, that's just begging for code injection.

Another way to do this that I don't see listed here is using:

let message = JSON.stringify(your_json_here); // "Hello World"
console.log(JSON.parse(message))              // Hello World
3

I also had this question, but in my case I didn't want to use a regex, because my JSON value may contain quotation marks. Hopefully my answer will help others in the future.

I solved this issue by using a standard string slice to remove the first and last characters. This works for me, because I used JSON.stringify() on the textarea that produced it and as a result, I know that I'm always going to have the "s at each end of the string.

In this generalized example, response is the JSON object my AJAX returns, and key is the name of my JSON key.

response.key.slice(1, response.key.length-1)

I used it like this with a regex replace to preserve the line breaks and write the content of that key to a paragraph block in my HTML:

$('#description').html(studyData.description.slice(1, studyData.description.length-1).replace(/\\n/g, '<br/>'));

In this case, $('#description') is the paragraph tag I'm writing to. studyData is my JSON object, and description is my key with a multi-line value.

1
  • This is exactly what I was just working on...very helpful, thanks. :)
    – dav
    Apr 5, 2017 at 14:57
3

You can simple try String(); to remove the quotes.

Refer the first example here: https://www.w3schools.com/jsref/jsref_string.asp

Thank me later.

PS: TO MODs: don't mistaken me for digging the dead old question. I faced this issue today and I came across this post while searching for the answer and I'm just posting the answer.

1
  • Perfet! Use String() instead of JSON.stringify()
    – jcom
    Jan 13, 2021 at 20:02
1

What you are doing is making a JSON string in your example. Either don't use the JSON.stringify() or if you ever do have JSON data coming back and you don't want quotations, Simply use JSON.parse() to remove quotations around JSON responses! Don't use regex, there's no need to.

0

I dont think there is a need to replace any quotes, this is a perfectly formed JSON string, you just need to convert JSON string into object.This article perfectly explains the situation : Link

Example :

success: function (data) {

        // assuming that everything is correct and there is no exception being thrown
        // output string {"d":"{"username":"hi","email":"[email protected]","password":"123"}"}
        // now we need to remove the double quotes (as it will create problem and 
        // if double quotes aren't removed then this JSON string is useless)
        // The output string : {"d":"{"username":"hi","email":"[email protected]","password":"123"}"}
        // The required string : {"d":{username:"hi",email:"[email protected]",password:"123"}"}
        // For security reasons the d is added (indicating the return "data")
        // so actually we need to convert data.d into series of objects
        // Inbuilt function "JSON.Parse" will return streams of objects
        // JSON String : "{"username":"hi","email":"[email protected]","password":"123"}"
        console.log(data);                     // output  : Object {d="{"username":"hi","email":"[email protected]","password":"123"}"}
        console.log(data.d);                   // output : {"username":"hi","email":"[email protected]","password":"123"} (accessing what's stored in "d")
        console.log(data.d[0]);                // output : {  (just accessing the first element of array of "strings")
        var content = JSON.parse(data.d);      // output : Object {username:"hi",email:"[email protected]",password:"123"}" (correct)
        console.log(content.username);         // output : hi 
        var _name = content.username;
        alert(_name);                         // hi

}
0

I had similar situation in a Promise just solved doing a cast as (String)

export async function getUserIdByRole(userRole: string): Promise<string> {
  const getRole = userData.users.find((element) => element.role === userRole);

  return String (getRole?.id);
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.