4 Things You Must Do When Putting HTML in JSON:
1) Escape quotation marks used around HTML attributes like so
<img src=\"someimage.png\" />
2) Escape the forward slash in HTML end tags.
<div>Hello World!<\/div>.
This is an ancient artifact of an old HTML spec that didn't want HTML parsers to get confused when putting strings in a<SCRIPT>
tag. For some reason, today’s browsers still like it.3) This one was totally bizarre. You should include a space between the tag name and the slash on self-closing tags. I have no idea why this is, but on MOST modern browsers, if you try using javascript to append a
<li>
tag as a child of an unordered list that is formatted like so:<ul/>
, it won't work. It gets added to the DOM after the ul tag. But, if the code looks like this:<ul />
(notice the space before the /), everything works fine. Very strange indeed.4) Be sure to encode any quotation marks that might be included in (bad) HTML content. This is the only thing that would really break the JSON by accidentally terminating the string early. Any
"
characters should be encoded as"
if it is meant to be included as HTML content.