HTML to JSON
This library is capable to convert HTML string/element to JSON/JS Object or JSON to HTML.
Features
- Convert HTML to JSON or JavaScript Object
- Convert JSON or JavaScript Object to HTML
Example
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
Convert HTML to JSON or JavaScript Object
import { HTMLToJSON } from 'html-to-json-parser';
const { HTMLToJSON } = require('html-to-json-parser');
const element = '<div><ul><li>Hello <strong>World</strong></li></ul></div>';
const element = document.querySelector('div');
let result = await HTMLToJSON(element, true);
Convert JSON to HTML using JavaScript
import { JSONToHTML } from 'html-to-json-parser';
const { JSONToHTML } = require('html-to-json-parser');
const data = {
type: "div",
attributes: {
class: "container"
},
content: [
{
type: "ul",
content: [
{
type: "li",
content: [
"Hello ",
{
type: "strong",
content: [
"World"
]
}
]
}
]
}
]
};
let result = await JSONToHTML(data, false);
Convert JSON to HTML using TypeScript
import { JSONToHTML, JSONType } from 'html-to-json-parser';
const { JSONToHTML, JSONType } = require('html-to-json-parser');
const data: JSONType = {
type: "div",
attributes: {
class: "container"
},
content: [
{
type: "ul",
content: [
{
type: "li",
content: [
"Hello ",
{
type: "strong",
content: [
"World"
]
}
]
}
]
}
]
};
let result = await JSONToHTML(data, false);
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