Xerobit

HTML to JSON Converter

Paste any HTML markup to convert it into a structured JSON tree representing every element, attribute, text node and comment. This html to json tool runs entirely in your browser using a hand-written tokenizer, so nothing is uploaded to a server.

Paste the HTML markup you want to convert.

JSON output

[
  {
    "type": "element",
    "tag": "p",
    "attributes": {
      "class": "intro"
    },
    "children": [
      {
        "type": "text",
        "text": "Hello world"
      }
    ]
  }
]

How to use the html to json converter

  1. Paste your HTML markup into the input area.
  2. The converter parses the markup instantly as you type or paste.
  3. Review the resulting JSON tree showing every element, attribute and text node.
  4. Copy the JSON output for use in your application or API.

How the html to json conversion works

The converter uses a hand-written tokenizer that reads the HTML string character by character. It identifies opening tags, closing tags, attributes, text content, comments and doctype declarations without relying on the browser DOM.

Each parsed piece becomes a JSON object. Elements have a tag property, an attributes object and a children array. Text nodes store their content under a text key. For example, the markup <p class="note">Hi</p> produces a JSON object with type element, tag p, an attributes object containing class note, and a children array holding one text node with the value Hi.

This structure mirrors the logical document tree, making it straightforward to traverse programmatically.

Handling raw text elements

Certain HTML elements like <script> and <style> contain raw text where angle brackets are not treated as tag delimiters. The parser recognizes these elements and reads everything until the matching closing tag as plain text content.

This means JavaScript expressions such as if (a < b) inside a script block are preserved exactly as written rather than being misinterpreted as malformed HTML tags. The raw text is stored in the JSON output under the element's children array as a text node.

Void elements and self-closing tags

HTML defines several void elements that cannot have child content: area, base, br, col, embed, hr, img, input, link, meta, param, source, track and wbr.

The parser treats these elements specially. When it encounters a void element, it creates the node with its attributes but does not look for a closing tag or attempt to nest children inside it. Self-closing syntax like <br /> is also handled correctly for any element.

Entity decoding

HTML uses character entities to represent special characters. The parser decodes common named entities such as &amp;, &lt;, &gt;, &quot; and &apos; back into their literal characters. Numeric character references in both decimal (&#65;) and hexadecimal (&#x41;) formats are also decoded.

This ensures the JSON output contains the actual text content rather than escaped entity strings, which is usually what downstream applications expect when processing converted data.

When to use this format

Converting HTML to JSON is useful when you need to process web markup in environments that work natively with JSON, such as REST APIs, configuration files or data pipelines. Instead of passing raw HTML strings between services, a structured tree allows each consumer to query specific elements or attributes directly.

If you only need to extract tabular data from HTML tables, a dedicated html table to json tool may be more appropriate. For stripping markup entirely, consider html to text. And if your goal is to adapt markup for React components, html to jsx handles the attribute naming differences automatically.

Common edge cases

Real-world HTML is often imperfect. The parser handles several common irregularities gracefully:

  • Unclosed tags are left open in the tree rather than causing a failure, and elements whose end tag is optional (li, p, td, tr, dt, dd, option) are ended by the next sibling that opens.
  • A stray < that does not start a tag, as in 1 < 2, is kept as text.
  • Runs of whitespace in text nodes are collapsed to one space, except inside pre, textarea, script and style, which keep their text exactly.
  • Tag and attribute names are lower-cased as in an HTML parser, except inside svg, where names such as viewBox and linearGradient keep their case.
  • Attributes without values, such as disabled or checked, are stored with an empty string value.
  • Mismatched quotes around attribute values are tolerated where possible.
  • Doctype declarations are captured as their own node type rather than being discarded.

However, extremely malformed markup may produce a tree that differs from how a full browser engine would render it, since this tool prioritizes predictable parsing over error recovery heuristics.

Worked examples

ExampleInputResult
Simple paragraphHTML input: <p class="intro">Hello world</p>[ / { / "type": "element", / "tag": "p", / "attributes": { / "class": "intro" / }, / "children": [ / { / "type": "text", / "text": "Hello world" / } / ] / } / ]
Nested listHTML input: <ul><li>One</li><li>Two</li></ul>[ / { / "type": "element", / "tag": "ul", / "attributes": {}, / "children": [ / { / "type": "element", / "tag": "li", / "attributes": {}, / "children": [ / { / "type": "text", / "text": "One" / } / ] / }, / { / "type": "element", / "tag": "li", / "attributes": {}, / "children": [ / { / "type": "text", / "text": "Two" / } / ] / } / ] / } / ]
Void elements and commentHTML input: <!-- header --><img src="logo.png" alt="Logo"><br>[ / { / "type": "comment", / "content": " header " / }, / { / "type": "element", / "tag": "img", / "attributes": { / "src": "logo.png", / "alt": "Logo" / }, / "children": [] / }, / { / "type": "element", / "tag": "br", / "attributes": {}, / "children": [] / } / ]
Script tagHTML input: <script>var x = 1 < 2;</script>[ / { / "type": "element", / "tag": "script", / "attributes": {}, / "children": [ / { / "type": "text", / "text": "var x = 1 < 2;" / } / ] / } / ]

FAQ

Does this html to json tool upload my code?

No. All parsing happens locally in your browser using JavaScript. Your HTML markup is never sent to any server.

What happens to script and style contents?

The parser treats script and style elements as raw text containers. Everything between the opening and closing tags is preserved as a text node in the JSON output without attempting to parse inner angle brackets.

Are HTML entities decoded in the output?

Yes. Named entities like &amp; and numeric references like &#65; are decoded to their actual characters in the resulting JSON text nodes.

How are void elements like img and br represented?

Void elements appear as nodes with their tag name and attributes but with an empty children array, since they cannot contain nested content in HTML.

Can I convert JSON back to HTML?

This tool performs one-way conversion from HTML to JSON. To go the other direction, you would need to write a serializer that walks the JSON tree and reconstructs the markup.

Is the output valid JSON?

Yes. The result is standard JSON that can be parsed by any JSON library in any programming language.