DOM to JsonML Converter
Convert HTML DOM structures to JsonML array format instantly. Paste HTML, get valid JsonML output with attributes, children, and text nodes preserved.
About
JsonML (JSON Markup Language) represents DOM trees as nested arrays: the tag name occupies index 0, an optional attributes object sits at index 1, and child nodes fill the remaining positions. Getting the conversion wrong - dropping attributes, collapsing whitespace text nodes, or mishandling self-closing tags - produces arrays that cannot round-trip back to valid markup. This tool parses your HTML through the browser's native DOMParser, then performs a recursive depth-first traversal with configurable filters for comments, whitespace, and node exclusion. It approximates standard JsonML per jsonml.org, assuming well-formed input; malformed HTML is silently corrected by the parser, which may rearrange nodes.
Formulas
The DOM-to-JsonML conversion follows a recursive traversal algorithm. For each node N in the DOM tree:
Where tag = N.tagName.toLowerCase(), attrs is a plain object built from N.attributes (omitted if empty), and children is the ordered list of N.childNodes. The recursion depth is bounded by dmax = 512 to prevent stack overflow on pathological input. Whitespace-only text nodes matching the pattern RegExp(/^\s+$/) are excluded when trimming is enabled.
Reference Data
| DOM Node Type | nodeType Value | JsonML Representation | Example Input | Example Output |
|---|---|---|---|---|
| Element | 1 | ["tag", {attrs}, ...children] | <div class="x">hi</div> | ["div",{"class":"x"},"hi"] |
| Text | 3 | "string" | Hello world | "Hello world" |
| Comment | 8 | ["!--", "text"] (optional) | <!-- note --> | ["!--"," note "] |
| CDATA Section | 4 | "string" (text content) | <![CDATA[data]]> | "data" |
| Document | 9 | Children processed from body | <html>...</html> | ["html", ...] |
| Document Fragment | 11 | Array of children | (fragment) | [...children] |
| Processing Instruction | 7 | Skipped | <?xml ...?> | (omitted) |
| Attribute (standalone) | 2 | Key-value in attrs object | id="main" | {"id":"main"} |
| Void Element | 1 | ["tag", {attrs}] (no children) | <br/> | ["br"] |
| Whitespace-only Text | 3 | Omitted if trim enabled | \n \t | (omitted) |
| Nested Elements | 1 | Recursive arrays | <ul><li>A</li></ul> | ["ul",["li","A"]] |
| Multiple Attributes | 1 | All in attrs object | <a href="#" class="x"> | ["a",{"href":"#","class":"x"}] |
| Mixed Content | 1 | Strings + arrays interleaved | <p>A<b>B</b>C</p> | ["p","A",["b","B"],"C"] |
| Empty Element | 1 | ["tag"] or ["tag", {}] | <span></span> | ["span"] |
| Boolean Attribute | 2 | {"attr": ""} | <input disabled> | ["input",{"disabled":""}] |
Frequently Asked Questions
or tags).