html2json's convert html to json library.
npm install node-html2json
Convert HTML to JSON library.
``javascript
let toJson = require("node-html2json");
//return a json object
toJson(html, mapping);
`
html2json is designed to convert html to json object, JQuery like.
`javascript
let mapping = {
title: "head>title"
};
//json = {"title": "THE html title"}
let json = toJson(html, mapping);
`
get title element text by default.
`javascript`
{
title: "head>title";
}
to get attribute 'href'.
`javascript`
{
title: {
selector: "some Tag",
attr: "href"
}
}
use foreach like this.
`javascript`
const mapping = {
results: {
selector: ".result",
foreach: {
title: ".c-title>a",
url: {
selector: ".c-title>a",
attr: "href"
}
}
}
};
object value could be a function with an parameter "element", which is an cheerio object.
`javascript`
const mapping = {
title: function(element) {
let text = element
.find("head>title")
.text()
.trim();
return text.split("_")[0];
}
};
use "." to represent the current element.
`javascript`
const mapping = {
results: {
selector: ".result",
foreach: {
title: ".c-title>a",
id: {
selector: ".",
attr: "id"
}
}
}
};
`shell``
yarn test