Skip to main content

Minimal DSL for API data extraction

Project description

Siphon

A minimal DSL for extracting data from JSON APIs.

Like a siphon draws liquid from a container, Siphon draws the data you need from nested JSON structures—just define the paths, and let it flow.

Install

pip install siphon-dsl

Or with uv:

uv add siphon-dsl

Quick Start

from siphon import process

data = {
    "data": {
        "id": "prod_123",
        "items": [
            {"id": 1, "status": "active", "name": "Widget"},
            {"id": 2, "status": "inactive", "name": "Gadget"},
            {"id": 3, "status": "active", "name": "Thing"},
        ],
    }
}

spec = {
    "extract": {
        "id": "$.data.id",
        "all_active": {
            "path": "$.data.items[*]",
            "where": {"status": "active"},
            "select": {"item_id": "id", "item_name": "name"},
            "collect": True,
        },
    }
}

result = process(spec, data)

Output:

{
  "id": "prod_123",
  "all_active": [
    {"item_id": 1, "item_name": "Widget"},
    {"item_id": 3, "item_name": "Thing"}
  ]
}

Features

Feature Syntax Description
Simple paths $.data.id Extract nested values
Array iteration $.items[*].name Traverse arrays
Filtering where: {status: "active"} Filter by field values
Ancestor filtering where: {parentId: 123} Filter by parent-level properties
Projection select: {new: "old"} Rename and reshape fields
Coalesce select: {price: "sale || list"} First non-null path wins (SQL-style ||)
Collect collect: true Return all matches (default: first only)
Reduce reduce: "min_time" Aggregate array values to a single result

Spec Format

Simple extraction

extract:
  id: "$.data.id"
  name: "$.data.name"

Extended extraction

extract:
  active_items:
    path: "$.data.items[*]"
    where: {status: "active"}
    select: {item_id: "id", item_name: "name"}
    collect: true

Coalesce in select

A select path string may contain || to declare a fallback chain. Segments are tried left-to-right; the first one resolving to a non-None value wins; otherwise the field is None. Any number of segments is supported.

extract:
  products:
    path: "$.products[*]"
    select:
      id: id
      price: "sale_price || list_price"   # fall back to list_price if sale_price is null
      label: "customLabel || title || ticketCategory"
    collect: true

Reduce (aggregation)

extract:
  earliest_slot:
    path: "$.items[*].from_datetime"
    reduce: min_time          # earliest time-of-day across all items

  latest_slot:
    path: "$.items[*].to_datetime"
    reduce: max_time          # latest time-of-day across all items

  total_price:
    path: "$.items[*].price"
    reduce: sum

  unique_categories:
    path: "$.items[*].category"
    reduce: distinct

Available operators:

Operator Description
min_time / max_time Earliest/latest time-of-day — accepts ISO 8601 datetime or plain HH:MM / HH:MM:SS (ignores date + timezone)
min_date / max_date Earliest/latest calendar date (ignores time)
min_datetime / max_datetime Earliest/latest full datetime (timezone-normalised)
min_int / max_int Minimum/maximum numeric value
sum Sum of numeric values
count Count of non-null values (returns 0 for empty)
first / last First or last value in traversal order
concat Join values as a string (default separator ", ")
distinct Deduplicated list, preserving first-seen order

For concat with a custom separator use the dict form:

"reduce": {"op": "concat", "sep": " | "}

Fetch from API

from siphon import fetch_and_process

spec = {
    "request": {"path": "/products"},
    "extract": {
        "id": "$.data.id",
        "names": {"path": "$.data.items[*].name", "collect": True},
    },
}

result = fetch_and_process(spec, "https://api.example.com")

Requires requests:

pip install siphon-dsl[http]

Typed Specs (Pydantic)

from siphon.typed import process_spec, ExtractSpec, FieldSpec

spec = ExtractSpec(
    extract={
        "id": "$.data.id",
        "active_items": FieldSpec(
            path="$.data.items[*]",
            where={"status": "active"},
            select={"item_id": "id", "name": "name"},
            collect=True,
        ),
    }
)

result = process_spec(spec, data)

Requires pydantic:

pip install siphon-dsl[typed]

Why Siphon?

  • Minimal — ~100 lines of code, no dependencies
  • Declarative — specs are data, not code
  • Composable — combine paths, filters, and projections

Spec History

See specs/ for version history and full documentation. Latest: v0.10select accepts a list of paths for coalesce/fallback.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

siphon_dsl-0.10.0.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

siphon_dsl-0.10.0-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file siphon_dsl-0.10.0.tar.gz.

File metadata

  • Download URL: siphon_dsl-0.10.0.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for siphon_dsl-0.10.0.tar.gz
Algorithm Hash digest
SHA256 4806054dbee766bc8214e2ebf93c237464250ba60a139fafa8d274559d035989
MD5 0576163ddc5a0850ed9461bb99859cc8
BLAKE2b-256 87c146e92bed642b7587caddf5eb233e4e822248104838e083989d05c3d1e906

See more details on using hashes here.

Provenance

The following attestation bundles were made for siphon_dsl-0.10.0.tar.gz:

Publisher: workflow.yml on alpeshvas/siphon

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file siphon_dsl-0.10.0-py3-none-any.whl.

File metadata

  • Download URL: siphon_dsl-0.10.0-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for siphon_dsl-0.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5a6d5ed6e16edfaaa94a44393c68eba00dbcdc5e55d787c79f5aeca673ddb999
MD5 0497232b231eea159a1a3971eda9b3ee
BLAKE2b-256 1c97b26b8204c0d90d1641b92df57e160b0e3f94f64106967c57efa0b453152a

See more details on using hashes here.

Provenance

The following attestation bundles were made for siphon_dsl-0.10.0-py3-none-any.whl:

Publisher: workflow.yml on alpeshvas/siphon

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page