0

How to convert this to JS object:

{"info":["Image has been added"],"success":["No success Sorry!"]}

JSON.parse throws an error and says &quot isn't valid JSON. Am new to JS and can't figure out how to deal with this.

I am using connect-flash from express to transfer flash messages to the client side. For doing so, I am using the following code:

if Object.keys(locals.flashes).length !== 0
    script.
      flashArr="#{JSON.stringify(locals.flashes)}"

On the client side, the representation of the Object isn't valid JSON. How can I handle this.

Aditya Raj
  • 327
  • 3
  • 14
  • Cannot replicate, no errors here https://jsfiddle.net/83vhvv5e/ – CertainPerformance May 02 '18 at 02:45
  • `{"info":["Image has been added"],"success":["No success Sorry!"]}` this is an valid js object already, doesn't require to be convert – Dean May 02 '18 at 02:45
  • there is no `&quot` in what you posted - so clearly what you posted is **not** what you are working with - `"` is the HTMLEntitity known as `"` – Jaromanda X May 02 '18 at 02:46
  • An error is there when I use JSON.stringify(locals.flashes) and get the variable on the client side. Then when using JSON.parse(flashArr), I get the error – Aditya Raj May 02 '18 at 02:48
  • {"info":["Image has been added"],"success":["No success Sorry!"]} – Aditya Raj May 02 '18 at 02:48
  • aha, now we see the culprit - that's not JSON – Jaromanda X May 02 '18 at 02:49
  • Plz check it now. The editor had converted into a simple sentence – Aditya Raj May 02 '18 at 02:50
  • But that is what i get on using JSON.stringify on flashArr – Aditya Raj May 02 '18 at 02:51
  • https://stackoverflow.com/questions/10080208/parsing-json-with-special-characters – Dr Upvote May 02 '18 at 02:52
  • 1
    No it is not, that's what happens once it is sent/received (not sure which end is doing this) ... check this out though `var x = document.createElement('div'); x.innerHTML = '{"info":["Image has been added"],"success":["No success Sorry!"]}'; console.log(JSON.parse(x.textContent))` - may help you, but as you've shown very little actual code, I can't do better – Jaromanda X May 02 '18 at 02:52

2 Answers2

2

How to convert this to JS object?

If I try like this it returns me valid JS object. I just globally replaced the " with "" using String.prototyp.replace and then parse it using JSON.parse() like this

invalid_data = '{"info":["Image has been added"],"success":["No success Sorry!"]}';
valid_data = invalid_data.replace(/"/g, '"');
console.log(JSON.parse(valid_data));
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
0

As suggested, you need to parse the HTML entities to characters, i.e. convert " to ". The simplest way is to use a regular expression:

var str = '{"info":["Image has been added"],"success":["No success Sorry!"]}';

var obj = JSON.parse(str.replace(/"/g,'"'));
console.log(obj);

Of course if you have other HTML entities in the code it might be better to use the built-in HTML parser:

var str = '{"info":["Image has been added"],"success":["No success Sorry!"]}';

var el = document.createElement('div');
el.innerHTML = str;

var obj = JSON.parse(el.textContent);
console.log(obj);

But for a single entity, replace is likely faster and more efficient.

RobG
  • 142,382
  • 31
  • 172
  • 209