Easily convert JSON to HTML and HTML to JSON using two javascript functions:
JSON_to_HTML()
// and
HTML_to_JSON()
Pass JSON string or object
JSON_to_HTML([
{
tagName: "div",
id: "main",
children: [
{
tagName: "h1",
id: "mainTitle",
children: [
{ text: "Hello World!" }
]
}
]
}
]);
Output:
<div id="main"><h1 id="mainTitle">Hello World!</h1></div>
Pass HTML string
HTML_to_JSON(`<div id="main"><h1 id="mainTitle">Hello World!</h1></div>`);
Output:
{
tagName: "html",
children: [
{
tagName: "head"
},
{
tagName: "body",
children: [
{
tagName: "div",
id: "main",
children: [
{
tagName: "h1",
id: "mainTitle",
children: [
{ text: "Hello World!" }
]
}
]
}
]
}
]
}
You can set the 'strip' parameter to only return the children of the body
HTML_to_JSON(`<div id="main"><h1 id="mainTitle">Hello World!</h1></div>`);
Output:
[
{
tagName: "div",
id: "main",
children: [
{
tagName: "h1",
id: "mainTitle",
children: [
{ text: "Hello World!" }
]
}
]
}
]