Skip to content

API Reference parrot.utils.jsonld_extractors

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.utils.jsonld_extractors

View source

JSON-LD extractor functions and data model for WebScrapingLoader.

This module provides:

  • JsonLdItem — dataclass representing a single extracted JSON-LD item
  • strip_html_text — utility to decode HTML entities and strip tags
  • One extractor function per supported schema.org @type
  • EXTRACTOR_REGISTRY — dict mapping @type strings to extractor callables

Extractor functions are pure data transformations: they receive a parsed JSON-LD node (a dict) and return List[JsonLdItem]. They have no dependency on WebScrapingLoader or BeautifulSoup beyond tag-stripping.

Dispatch into the loader's pipeline happens in webscraping.py via _extract_jsonld and _walk_jsonld_node.

Attribute EXTRACTOR_REGISTRY

Type: Dict[str, Callable[[Dict[str, Any]], List[JsonLdItem]]]

Class JsonLdItem

A single structured item extracted from a JSON-LD block.

Parameter Type Default Description
content_kind str
source_type str
page_content str
row_data Dict[str, Any] dict()
selector_name Optional[str] None

Fields:

Field Type Default Description
content_kind str Semantic type label (e.g. "faq", "jsonld-product").
source_type str Provenance label (e.g. "faq-jsonld", "product-jsonld").
page_content str Plain-text representation optimised for embedding.
row_data Dict[str, Any] field(default_factory=dict) Raw key/value data for downstream metadata.
selector_name Optional[str] None Human-readable name used as the selector_name metadata field. Defaults to content_kind when not explicitly set.

Function article_extractor

Extract Article / NewsArticle / BlogPosting data from a JSON-LD node.

article_extractor(node: Dict[str, Any]) -> List[JsonLdItem]

Parameter Type Default Description
node Dict[str, Any] Parsed JSON-LD dict with @type in {"Article", "NewsArticle", "BlogPosting"}.

Returns: List[JsonLdItem] — List with one JsonLdItem (empty list if neither headline nor name is present).

Function breadcrumb_extractor

Extract BreadcrumbList data from a JSON-LD node.

Emits ONE item with the full breadcrumb path as page_content (e.g. "Home > Products > Widget Pro").

breadcrumb_extractor(node: Dict[str, Any]) -> List[JsonLdItem]

Parameter Type Default Description
node Dict[str, Any] Parsed JSON-LD dict with @type="BreadcrumbList".

Returns: List[JsonLdItem] — List with one JsonLdItem (empty list if no crumbs found).

Function event_extractor

Extract Event data from a JSON-LD node.

event_extractor(node: Dict[str, Any]) -> List[JsonLdItem]

Parameter Type Default Description
node Dict[str, Any] Parsed JSON-LD dict with @type="Event".

Returns: List[JsonLdItem] — List with one JsonLdItem (empty list if name is absent).

Function faq_extractor

Extract FAQ Q&A pairs from a FAQPage JSON-LD node.

Yields one JsonLdItem per Question/Answer pair with:

  • content_kind="faq"
  • source_type="faq-jsonld"
  • page_content="Q: <question>\n\nA: <answer>"

Backward-compatible with the original _iter_faqpage_pairs / _docs_from_faqpage pipeline.

faq_extractor(node: Dict[str, Any]) -> List[JsonLdItem]

Parameter Type Default Description
node Dict[str, Any] Parsed JSON-LD dict with @type="FAQPage".

Returns: List[JsonLdItem] — List of JsonLdItem instances (empty if no valid Q&A pairs found).

Function howto_extractor

Extract HowTo data from a JSON-LD node.

howto_extractor(node: Dict[str, Any]) -> List[JsonLdItem]

Parameter Type Default Description
node Dict[str, Any] Parsed JSON-LD dict with @type="HowTo".

Returns: List[JsonLdItem] — List with one JsonLdItem (empty list if name is absent).

Function organization_extractor

Extract Organization data from a JSON-LD node.

organization_extractor(node: Dict[str, Any]) -> List[JsonLdItem]

Parameter Type Default Description
node Dict[str, Any] Parsed JSON-LD dict with @type="Organization".

Returns: List[JsonLdItem] — List with one JsonLdItem (empty list if name is absent).

Function person_extractor

Extract Person data from a JSON-LD node.

person_extractor(node: Dict[str, Any]) -> List[JsonLdItem]

Parameter Type Default Description
node Dict[str, Any] Parsed JSON-LD dict with @type="Person".

Returns: List[JsonLdItem] — List with one JsonLdItem (empty list if name is absent).

Function place_extractor

Extract Place / LocalBusiness data from a JSON-LD node.

place_extractor(node: Dict[str, Any]) -> List[JsonLdItem]

Parameter Type Default Description
node Dict[str, Any] Parsed JSON-LD dict with @type in {"Place", "LocalBusiness", "Restaurant"}.

Returns: List[JsonLdItem] — List with one JsonLdItem (empty list if name is absent).

Function product_extractor

Extract Product data from a JSON-LD node.

product_extractor(node: Dict[str, Any]) -> List[JsonLdItem]

Parameter Type Default Description
node Dict[str, Any] Parsed JSON-LD dict with @type="Product" or similar.

Returns: List[JsonLdItem] — List with one JsonLdItem (empty list if name is absent).

Function question_extractor

Extract a bare top-level Question node.

The JSON-LD spec permits a @type="Question" node to appear at the top level of a block (i.e. not nested inside a FAQPage.mainEntity). This extractor preserves backward compatibility with the legacy _iter_faqpage_pairs behaviour that handled this case explicitly.

Produces one JsonLdItem with the same content_kind="faq" and source_type="faq-jsonld" as :func:faq_extractor so downstream consumers see a uniform FAQ item regardless of nesting shape.

question_extractor(node: Dict[str, Any]) -> List[JsonLdItem]

Parameter Type Default Description
node Dict[str, Any] Parsed JSON-LD dict with @type="Question".

Returns: List[JsonLdItem] — List with one JsonLdItem (empty list if question or answer is absent or blank).

Function recipe_extractor

Extract Recipe data from a JSON-LD node.

recipe_extractor(node: Dict[str, Any]) -> List[JsonLdItem]

Parameter Type Default Description
node Dict[str, Any] Parsed JSON-LD dict with @type="Recipe".

Returns: List[JsonLdItem] — List with one JsonLdItem (empty list if name is absent).

Function strip_html_text

Render arbitrary text as clean plain text.

Replicates WebScrapingLoader._strip_html exactly:

  1. HTML-unescape entities (&amp; → &, &nbsp; → space, …).
  2. Strip HTML tags via BeautifulSoup so nested anchors/lists collapse to their visible text.
  3. Collapse whitespace runs (including \xa0 from &nbsp;) to a single space and strip leading/trailing whitespace.

strip_html_text(text: Any) -> str

Parameter Type Default Description
text Any Any value. None returns ""; non-strings are coerced to str before processing.

Returns: str — Cleaned plain-text string.

Function walk_jsonld

Recursively walk a JSON-LD structure dispatching typed nodes to extractors.

This is the single authoritative implementation of the JSON-LD graph traversal algorithm, shared by WebScrapingLoader._walk_jsonld_node and the executor's _action_extract_jsonld. Both callers delegate here so that any future fix or extension only needs to be applied once.

Handles:

  • Top-level arrays of nodes.
  • @graph containers (Google's recommended form).
  • Single typed objects dispatched via :data:EXTRACTOR_REGISTRY.
  • Nodes with @type as a list (valid per JSON-LD spec).

Declaration order of EXTRACTOR_REGISTRY determines priority: when a node carries multiple @type values the first matching key wins.

walk_jsonld(data: Any, items: List[JsonLdItem], allowed_types: Optional[set] = None) -> None

Parameter Type Default Description
data Any Parsed JSON-LD value (dict, list, or scalar). Scalars are silently ignored.
items List[JsonLdItem] Accumulator list; extracted :class:JsonLdItem instances are appended in-place.
allowed_types Optional[set] None Optional whitelist of @type strings. None (default) means all registered types are extracted. An empty set() disables extraction for the subtree.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally