Skip to main content

Printing out Literal Characters with HTML

Escaping and Un-Escaping Characters

Due to the nature of some of the character used for special purposes in the programming languages such as \ or ", if you want to type a literal version of that special character you must use the backslash (\) character to un-escape it so that the compiler / interpreter will interpret them as literals and not the special character.

This leads to a pretty confusing state of sending say a text blob that contains those special characters, especially a JSON encoded HTML text. For example:

{
  "text": "<html lang=\"en\"><p>hi there!</p>\r\n<h1>o hoi!</h1></html>"
}

As you can see this is suppose to render to this following HTML page, but you can see that the double quotes is escaped as well as the returning carriage and new line character.

<html lang="en">
  <p>hi there!</p>
  <h1>o hoi!</h1>
</html>

Converting JSON escaped HTML text to HTML

If you would like to convert the JSON escaped HTML text back to HTML, you can just use JSON library to deserialize the JSON String literal back to String. For example if you have the following input file

<html lang=\"en\"><p>hi there!</p>\r\n<h1>o hoi!</h1></html>

You can use Python to deserialize and convert the String back into HTML again:

import json

with open("input") as f:
  print(json.load(f))

This will interpret the \" as " as literal, \r\n as new lines for your String.