2.1.1 • Published 5 years ago

node-html2json v2.1.1

Weekly downloads
38
License
MIT
Repository
github
Last release
5 years ago

html2json -- Convert Html to Json

NPM

Convert HTML to JSON library.

Simple to use

let toJson = require("node-html2json");

//return a json object
toJson(html, mapping);

html2json is designed to convert html to json object, JQuery like.

let mapping = {
  title: "head>title"
};

//json = {"title": "THE html title"}
let json = toJson(html, mapping);

Attribute

get title element text by default.

{
  title: "head>title";
}

to get attribute 'href'.

{
  title: {
    selector: "some <a> Tag",
    attr: "href"
  }
}

Foreach

use foreach like this.

const mapping = {
  results: {
    selector: ".result",
    foreach: {
      title: ".c-title>a",
      url: {
        selector: ".c-title>a",
        attr: "href"
      }
    }
  }
};

Function

object value could be a function with an parameter "element", which is an cheerio object.

const mapping = {
  title: function(element) {
    let text = element
      .find("head>title")
      .text()
      .trim();
    return text.split("_")[0];
  }
};

Current Element

use "." to represent the current element.

const mapping = {
  results: {
    selector: ".result",
    foreach: {
      title: ".c-title>a",
      id: {
        selector: ".",
        attr: "id"
      }
    }
  }
};

Test Useage

  yarn test
2.1.1

5 years ago

2.1.0

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.0.3

5 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago