109

I am trying to inject json into my backbone.js app. My json has " for every quote.

Is there a way for me to remove this?
I've provided a sample below:

[{"Id":1,"Name":"Name}]
4
  • 3
    Why not stop this from happening in the first place? At present you don't have JSON at all, but something that looks a little bit like it. Commented Feb 11, 2012 at 23:07
  • It was encoding the quotes. It is a totally different issue not related to Json, but related to ASP.NET MVC. We may want to delete this post. Commented Feb 11, 2012 at 23:18
  • use this instead of replace stackoverflow.com/questions/11147468/… Commented Oct 18, 2019 at 17:12
  • Since your comment mentions ASP.NET MVC, I'll throw in that the root cause of my issue was not using @Html.Raw(jsonFromModel) as mentioned in stackoverflow.com/questions/7493096/… Commented Jan 26, 2023 at 23:20

9 Answers 9

206

Presumably you have it in a variable and are using JSON.parse(data);. In which case, use:

JSON.parse(data.replace(/"/g,'"'));

You might want to fix your JSON-writing script though, because " is not valid in a JSON object.

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

5 Comments

When I try this, I saw my js code change to that: result.replace(/"/g,'"')
@kirlisakal were you able to get around this issue?
This works, but what if data has some value like this: 10"6 inch display?
Then you might want to fix your JSON-writing script.
I'm seeing this in an API that isn't mine. Why would it generate " instead of "? (I think it's a react app)
24

Accepted answer is right, however I had a trouble with that. When I add in my code, checking on debugger, I saw that it changes from

result.replace(/"/g,'"')

to

result.replace(/"/g,'"')

Instead of this I use that:

result.replace(/(&quot\;)/g,"\"")

By this notation it works.

1 Comment

Yes, it works! but I need to customize Session object to fix this :(
5
var data = $('<div>').html('[{&quot;Id&quot;:1,&quot;Name&quot;:&quot;Name}]')[0].textContent;

that should parse all the encoded values you need.

1 Comment

That was the best solution. I had lots of escape charecters. This trick unescaped all
3

This is a simple way to replace &quot with what you need to change it - chars, strings etc.

function solve(input) {
   const replaceWith = '"' // e.g. replace &quot; by "
   const result = input.replace(/&quot;/g, replaceWith)
   return result;
} 

console.log(solve('{&quot;x&quot;:&quot;1&quot;,&quot;y&quot;:&quot;2&quot;,&quot;z&quot;:&quot;10&quot;}')
   

Comments

1

The following works for me:

function decodeHtml(html) {
    let areaElement = document.createElement("textarea");
    areaElement.innerHTML = html;

    return areaElement.value;
}

1 Comment

It worked! The only change I would make is to remove the textarea at the end with areaElement.remove();
1

In my case "quot" was replaced with other characters so I found a stupid but working workaround

str.replace(/&qu/g,'').replace(/ot\;/g,'')

Comments

0

i used replace feature in Notepad++ and replaced &quot; (without quotes) with " and result was valid json

Comments

0

IF you are using SQL then you can use:

update tablename set payload = replace(payload, '&', chr(39)) where id = 'unique id';

Comments

-3

If you are using python then you can use:

import html
l = [{&quot;Id&quot;:1,&quot;Name&quot;:&quot;Name}]
r = html.unescape(l)

Comments

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.