Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/moinism/jsonfromhtml
NPM package to convert HTML to JSON
https://github.com/moinism/jsonfromhtml
html html2json javascript json node npm
Last synced: 24 days ago
JSON representation
NPM package to convert HTML to JSON
- Host: GitHub
- URL: https://github.com/moinism/jsonfromhtml
- Owner: moinism
- License: mit
- Created: 2020-04-25T17:14:21.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-04-26T16:04:08.000Z (over 4 years ago)
- Last Synced: 2024-04-25T11:43:30.604Z (7 months ago)
- Topics: html, html2json, javascript, json, node, npm
- Language: JavaScript
- Size: 6.84 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Turn HTML DOM to JSON-able structure
👌 Zero dependencies ⚡️ Ultra lightweight
```
npm i -S jsonfromhtml
``````js
const { jsonFromHTML } = require('jsonfromhtml')const body = jsonFromHTML(document.body) // returns object
const json = JSON.stringify(body)
```## Usage
```js
const element = jsonFromHTML(domElement)
```Parameter:
- `domElement`: Expects a DOM element. Use a package like [cheerio](https://www.npmjs.com/package/cheerio) to use it in node envoirnment.Returns:
- An `Object`.### Returned `Object` Example
Input:
```html
Here is my Github if you wanna have a looksy.
```Output:
```js
{
"nodeName": "P", // tag name
"nodeType": "element", // either 'element' or 'text'
"attrs": {}, // only for nodeType == 'element'
"children": [
{
"content": "\nHere is my ",
"nodeType": "text"
},
{
"nodeName": "A",
"nodeType": "element",
"attrs": { // key-value pairs of all the attributes on the element.
"href": "https://github.com/moinism",
"target": "_blank"
},
"children": [
{
"content": "Github",
"nodeType": "text"
}
]
},
{
"content": " if you wanna have a looksy.\n",
"nodeType": "text"
}
]
}
```### LICENSE
MIT