Putting HTML in JSON – Four Things You Must Do

There are 4 things you must do if you want to include HTML content in a JSON data structure. These items were originally included in a recent post about using XSLT to include HTML in JSON. However, this information is good for anyone to remember when creating JSON data, regardless of whether XSLT is used or not.

The JSON spec does not have many restrictions. JSON data is either a string, a number, a boolean, an object, an array, or null. As long as your data is formatted as one of these types, it should be valid. However, if your data contains HTML, there are certain things that you need to do to keep the browser happy when using your JSON data within Javascript.

  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 &quot; if it is meant to be included as HTML content.

 

Here is a fictional example of JSON data that shows each of these suggestions. This was based on a real-world example where list items (<li> tags), where added to the <ul> dynamically in Javascript.
{
    "ad_box": {
        "html": "<div class=\"ad_box\"><img class=\"banner\" src=\"some_ad.png\" alt=\"\" \/>
<h3>&quot;Hot&quot; Items <\/h3> <br \/> <ul id=\"items\" \/><\/div>",
        "item_id": 1234,
        "click_action":"/logAction?id=1234&amp;type=click&amp;info=blah",
        "icon":"http://my.images.com/icons/icon.png"
    }
}

Hopefully anyone attempting to serialize HTML as JSON will find these lessons-learned helpful.

Like this post? Please share it! Then follow us on Twitter – @thorntech – and sign up for our email list below for future updates.

[grwebform url=”https://app.getresponse.com/view_webform_v2.js?u=BK4gP&webforms_id=2855304″ css=”on” center=”off” center_margin=”200″/]