Can i use html tags in json?

  • Thorn Tech Staff
  • Jul 25, 2012
  • Web Development

Can i use html tags in json?

Can i use html tags in json?

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.

Can i use html tags in json?

Get insights on SFTP Gateway, cloud computing and more, in your inbox.

Get smarter about all things tech. Sign up now!

Discover SFTP Gateway

Learn more about SFTP Gateway

Search our blog

Recent posts

August 18, 2022 No Comments

August 3, 2022 No Comments

Can i use html tags in json?

This article is going to assume that you’re trying to insert HTML directly inside the JSON file.

The problem with HTML inside JSON is that HTML elements have properties that must use double quotes.

But as soon as you insert double quotes it’s going to cause syntax error because JSON can only do double quotes.

Can i use html tags in json?

If you’re interested why this breaks, here’s an article to explain why, “Types – String“.

Okay solution: Using HTML entities

You can use HTML entities to escape that syntax error.

Can i use html tags in json?

But it sometimes causes weird HTML side-effects.

Like adding double quotes around the double quotes. Or making link elements not work.

Best solution: Escape strings

The best solution to implement HTML with properties is to escape strings.

Can i use html tags in json?

Just add the backslash (\) symbol before the beginning of a double quote and the before the end of a double quote.

I like to tweet about JSON and post helpful code snippets. Follow me there if you would like some too!

What characters are not allowed in JSON?

The following characters are reserved characters and can not be used in JSON and must be properly escaped to be used in strings..
Backspace to be replaced with \b..
Form feed to be replaced with \f..
Newline to be replaced with \n..
Carriage return to be replaced with \r..
Tab to be replaced with \t..

How escape HTML tag JSON?

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. Escape the forward slash in HTML end tags. <div>Hello World!

How do I pass a json file in HTML?

Two options to consider if you are in this position are: encoding/decoding the markup first with something like [base64_encode][1] / decode (quite a bit of a performance hit), or (and perhaps preferably) be more selective in what you are passing via json, and generate the necessary markup on the client side instead.

How do I display data in JSON table in HTML?

Approach 1:.
Take the JSON Object in a variable..
Call a function which first adds the column names to the < table > element. (It is looking for the all columns, which is UNION of the column names)..
Traverse the JSON data and match key with the column name. ... .
Leave the column empty if there is no value of that key..