Skip to content

Tabular

tabular

Tabular strategy — convert homogeneous list[dict] to pipe-separated table.

TabularStrategy

Bases: SmeltStrategy

Convert JSON arrays of objects to pipe-separated tables.

Source code in packages/axm-smelt/src/axm_smelt/strategies/tabular.py
Python
class TabularStrategy(SmeltStrategy):
    """Convert JSON arrays of objects to pipe-separated tables."""

    @property
    def name(self) -> str:
        return "tabular"

    @property
    def category(self) -> str:
        return "structural"

    def apply(self, ctx: SmeltContext) -> SmeltContext:
        """Convert homogeneous ``list[dict]`` to pipe-separated tables.

        Uses ``ctx.parsed`` when available to skip
        ``json.loads``. Recurses into nested dicts to tabularize
        inner arrays.
        """
        parsed = ctx.parsed
        if parsed is None:
            text = ctx.text
            stripped = text.strip()
            if not stripped or stripped[0] not in ("[", "{"):
                return ctx
            try:
                parsed = json.loads(stripped)
            except (json.JSONDecodeError, ValueError):
                return ctx

        if isinstance(parsed, list):
            table = _to_table(parsed)
            if table is not None:
                new_ctx = SmeltContext(text=table, format=ctx.format)
                new_ctx._parsed = None  # table text is not JSON-parseable
                return new_ctx
            return ctx

        if isinstance(parsed, dict):
            result, changed = _tabularize_dict(parsed)
            if changed:
                return SmeltContext(
                    text=json.dumps(result, separators=(",", ":"), ensure_ascii=False),
                    format=ctx.format,
                )

        return ctx
apply(ctx)

Convert homogeneous list[dict] to pipe-separated tables.

Uses ctx.parsed when available to skip json.loads. Recurses into nested dicts to tabularize inner arrays.

Source code in packages/axm-smelt/src/axm_smelt/strategies/tabular.py
Python
def apply(self, ctx: SmeltContext) -> SmeltContext:
    """Convert homogeneous ``list[dict]`` to pipe-separated tables.

    Uses ``ctx.parsed`` when available to skip
    ``json.loads``. Recurses into nested dicts to tabularize
    inner arrays.
    """
    parsed = ctx.parsed
    if parsed is None:
        text = ctx.text
        stripped = text.strip()
        if not stripped or stripped[0] not in ("[", "{"):
            return ctx
        try:
            parsed = json.loads(stripped)
        except (json.JSONDecodeError, ValueError):
            return ctx

    if isinstance(parsed, list):
        table = _to_table(parsed)
        if table is not None:
            new_ctx = SmeltContext(text=table, format=ctx.format)
            new_ctx._parsed = None  # table text is not JSON-parseable
            return new_ctx
        return ctx

    if isinstance(parsed, dict):
        result, changed = _tabularize_dict(parsed)
        if changed:
            return SmeltContext(
                text=json.dumps(result, separators=(",", ":"), ensure_ascii=False),
                format=ctx.format,
            )

    return ctx