<commit_before># html2json -- Convert Html to Json

[![NPM](https://nodei.co/npm/node-html2json.png)](https://www.npmjs.com/package/node-html2json)

Convert HTML to JSON library.

## Simple to use

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

toJson(html, mapping)
```

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

```javascript
let mapping = {
  title:"head title",
};

let json = toJson(mapping);
```

## Attribute

`title:"head title"` get title element text by default. use `title:{selector:"head title", attr:"href"}` to get attributes.

## Foreach

use foreach like this. Point `.` mean current element.

```javascript
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. 

```javascript
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.

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

```<commit_msg>更新readme<commit_after># html2json -- Convert Html to Json

[![NPM](https://nodei.co/npm/node-html2json.png)](https://www.npmjs.com/package/node-html2json)

Convert HTML to JSON library.

## Simple to use

```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);
```

## Attribute

get title element text by default.

```json
title: "head>title"
```

to get attribute 'href'.

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

## Foreach

use foreach like this.

```javascript
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.

```javascript
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.

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

{'fraction_non_alphanumeric': 0.16684229012996135, 'fraction_numerical': 0.004214963119072708, 'mean_word_length': 2.880108991825613, 'pattern_counts': {'":': 1, '<': 4, '<?xml version=': 0, '>': 14, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6828193', 'n_tokens_mistral': 971, 'n_tokens_neox': 935, 'n_words': 268}