JSPM

html-to-json-parser

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 18213
  • Score
    100M100P100Q154732F
  • License MIT

This library converts HTML to JSON tree

Package Exports

  • html-to-json-parser

Readme

HTML to JSON

This library is capable to convert HTML string/element to JSON/JS Object.

From

    <div class="container">
      <ul>
          <li>Hello <strong>World</strong></li>
      </ul>
    </div>

To

    {
      "type": "div",
      "attributes": {
        "class": "container"
      },
      "content": [
        {
          "type": "ul",
          "content": [
            {
              "type": "li",
              "content": [
                "Hello ",
                {
                  "type": "strong",
                  "content": [
                    "World"
                  ]
                }
              ]
            }
          ]
        }
      ]
    }

Installation

Install html-to-json-parser with npm/yarn

  npm install html-to-json-parser // npm
  yarn add html-to-json-parser // yarn

Usage/Examples

Import

import HTMLParser from 'html-to-json-parser'; // ES6
const HTMLParser = require('html-to-json-parser'); // CommonJS

Data

const element = '<div><ul><li>Hello <strong>World</strong></li></ul></div>'; // HTML string
const element = document.querySelector('div'); // HTML element

Usage

let result = HTMLParser(element, true); // Default: false - true: return JSON, false: return JS Object

Contributing

  • Fork it!
  • Create your feature branch: git checkout -b my-new-feature
  • Commit your changes: git commit -am 'Add some feature'
  • Push to the branch: git push origin my-new-feature
  • Submit a pull request :D

Author

html-to-json-parser © Yousuf
Authored and maintained by Yousuf Kalim.

GitHub @yousufkalim · LinkedIn @yousufkalim

License

MIT