A Rust port of cheerio-json-mapper.
- Input: HTML source + Extractor spec (JSON)
- Output: JSON matching the structure defined in the spec
- Available as: Rust crate, CLI tool, and WebAssembly npm package
npm install @qretaio/html2jsoncargo install html2json --features clicargo install --path . --features cli
# or from a git repository
cargo install --git https://github.com/qretaio/html2json --features clijust installimport { extract } from "@qretaio/html2json";
const html = `
<article class="post">
<h2>My Article</h2>
<p class="author">John Doe</p>
<div class="tags">
<span>rust</span>
<span>wasm</span>
</div>
</article>
`;
const spec = {
title: "h2",
author: ".author",
tags: [
{
$: ".tags span",
name: "$",
},
],
};
const result = await extract(html, spec);
console.log(result);
// {
// "title": "My Article",
// "author": "John Doe",
// "tags": [{"name": "rust"}, {"name": "wasm"}]
// }# Extract from stdin (pipe from curl)
curl -s https://news.ycombinator.com/ | html2json --spec examples/hn.json
# Extract from stdin (pipe from cat)
cat examples/hn.html | html2json --spec examples/hn.json
# Check output matches expected JSON (useful for testing/CI)
cat examples/hn.html | html2json --spec examples/hn.json --check expected.json--spec, -s <FILE>- Path to JSON extractor spec file (required)--check, -c <FILE>- Compare output against expected JSON file. Exits with 0 if match, 1 if differ (with colored diff).
The spec is a JSON object where each key defines an output field and each value defines a CSS selector to extract that field.
{
"title": "h1",
"description": "p.description"
}{
"link": "a.main | attr:href",
"image": "img.hero | attr:src"
}{
"title": "h1 | trim",
"slug": "h1 | lower | regex:\\s+-",
"price": ".price | regex:\\$(\\d+\\.\\d+) | parseAs:int"
}Available pipes:
trim- Trim whitespacelower- Convert to lowercaseupper- Convert to uppercasesubstr:start:end- Extract substringregex:pattern- Regex capture (first group)parseAs:int- Parse as integerparseAs:float- Parse as floatattr:name- Get attribute valuevoid- Extract from void elements, useful for extracting xml
{
"items": [
{
"$": ".item",
"title": "h2",
"description": "p"
}
]
}{
"$": "article",
"title": "h1",
"paragraphs": ["p"]
}{
"title": "h1.main || h1.fallback || h1"
}{
"title": "h1",
"description?": "p.description"
}Optional fields that return null are removed from the output.
MIT