PROWARE technologies
PROWARE technologies

HTML to JSON Online Converter v.0.1

This converter is in beta but will correctly convert perfectly formed HTML to JSON. Currently measures are being taken to make it better at detecting malformed HTML.

Though this tool can convert a whole HTML document, it is not really intended for this, rather, it's for converting HTML structures with data like a table or an unordered list (HTML tag "UL").

HTML to Convert
JSON Output

This code will build the HTML/DOM elements from the created JSON code.

 // build DOM elements from JSON
function buildDOM(element, jsonObject) { // element is the parent element to add the children to
	if (typeof jsonObject == "string") {
		jsonObject = JSON.parse(jsonObject);
	}
	if (Array.isArray(jsonObject)) {
		for (var i = 0; i < jsonObject.length; i++) {
			buildDOM(element, jsonObject[i]);
		}
	}
	else {
		var e = document.createElement(jsonObject.tag);
		for (var prop in jsonObject) {
			if (prop != "tag") {
				if (prop == "children" && Array.isArray(jsonObject[prop])) {
					buildDOM(e, jsonObject[prop]);
				}
				else if (prop == "html") {
					e.innerHTML = jsonObject[prop];
				}
				else {
					e.setAttribute(prop, jsonObject[prop]);
				}
			}
		}
		element.appendChild(e);
	}
}
if (!Array.isArray) {
	Array.isArray = function (arg) {
		return Object.prototype.toString.call(arg) == "[object Array]";
	};
}