mathiasbynens / he

A robust HTML entity encoder/decoder written in JavaScript.
https://mths.be/he
MIT License
3.45k stars 254 forks source link

Unescape from HTML and escape to JSON #73

Closed KaKi87 closed 5 years ago

KaKi87 commented 5 years ago

The HTML code I wanna convert is formatted as JSON.

My JSON string contains HTML entities inside properties values, and especially ", which is the encoded character for ".

So, I need to escape the previously converted characters in JSON, which means adding a backslash (\) to them.

Thanks

mathiasbynens commented 5 years ago

Can you give an example?

Use JSON.stringify() to generate JSON (and take care of escaping quotes in strings).

KaKi87 commented 5 years ago

take care of escaping quotes in strings

That's exactly what I want to automate :)

mathiasbynens commented 5 years ago

Ok, I guess your issue is resolved then? :)

KaKi87 commented 5 years ago

No because I didn't found out how to automate string quote escaping

mathiasbynens commented 5 years ago

I'm confused. I just told you that JSON.stringify does that for you.

KaKi87 commented 5 years ago

JSON.parse(JSON.stringify(val)) returns val

mathiasbynens commented 5 years ago

It does indeed!

Again, can you give an example of what you're trying to do? Provide example input, desired output, and code you've tried so far.

KaKi87 commented 5 years ago

Here is a JSFiddle example : https://jsfiddle.net/KaKi87/56pbLtuf/

mathiasbynens commented 5 years ago

How is the content inserted into the <pre>? Are you doing that on the server side?

KaKi87 commented 5 years ago

No, this is an example. The original is a real JSON response, fetched using NodeJS request. But, this fiddle have the same behavior as in my script. The point is that I can't auto-escape quotes inside JSON strings.

mathiasbynens commented 5 years ago

The original is a real JSON response, fetched using NodeJS request.

If it's a proper JSON response, then JSON.parse would succeed, making it different from the demo you shared.

I'm still struggling to understand what you mean.

KaKi87 commented 5 years ago

Okay then try to parse this : https://trigedasleng.net/api/api?action=search&query=dei%20de

mathiasbynens commented 5 years ago

Works for me:

(async () => {
  const response = await fetch('https://trigedasleng.net/api/api?action=search&query=dei%20de');
  const data = await response.json();
  console.log('Result:', data);
})();
// Result: {...}
KaKi87 commented 5 years ago

Okay but did you decoded the HTML entities ?

mathiasbynens commented 5 years ago

To decode HTML entities in a string, use he.decode(string).

(async () => {
  const response = await fetch('https://trigedasleng.net/api/api?action=search&query=dei%20de');
  const data = await response.json();
  data.words = data.words.map((word) => {
    word.translation = he.decode(word.translation);
    return word;
  });
  console.log('Result:', data);
})();
// Result: {...}
KaKi87 commented 5 years ago

So, I must decode property by property ??

mathiasbynens commented 5 years ago

Yeah. There's no way he or any other library can know which values need decoding and which do not; it depends on the use case.

KaKi87 commented 5 years ago

All I need is to automatically escape the " character

David263 commented 5 years ago

It seems to me that you could use the string.replace method to replace any instance of " with \" in your server strings (if that is truly "all you need"). The he library is used to create and remove HTML entities, not to use \ to escape characters in strings.