How to use the html to json converter
- Paste your HTML markup into the input area.
- The converter parses the markup instantly as you type or paste.
- Review the resulting JSON tree showing every element, attribute and text node.
- 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.
Entity decoding
HTML uses character entities to represent special characters. The parser decodes common named entities such as &, <, >, " and ' back into their literal characters. Numeric character references in both decimal (A) and hexadecimal (A) 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 in1 < 2, is kept as text. - Runs of whitespace in text nodes are collapsed to one space, except inside
pre,textarea,scriptandstyle, which keep their text exactly. - Tag and attribute names are lower-cased as in an HTML parser, except inside
svg, where names such asviewBoxandlinearGradientkeep their case. - Attributes without values, such as
disabledorchecked, 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
| Example | Input | Result |
|---|---|---|
| Simple paragraph | HTML input: <p class="intro">Hello world</p> | [ / { / "type": "element", / "tag": "p", / "attributes": { / "class": "intro" / }, / "children": [ / { / "type": "text", / "text": "Hello world" / } / ] / } / ] |
| Nested list | HTML 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 comment | HTML 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 tag | HTML 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 & and numeric references like A 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.