Skip to content
suprbdevPublic

About

YAML-configured data transformation tool.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

alpakr

YAML-configured data transformation tool. Reads JSON, YAML, NDJSON or CSV from a file, URL, or stdin pipe, applies a pipeline of named handlers to restructure and transform the data, and outputs JSON, YAML, NDJSON or CSV to stdout or a file.

Useful for: migrating between schemas, importing externally sourced data, normalising nested/relational data.

Install

go install github.com/suprbdev/alpakr@latest
# or
make build   # outputs to ./bin/alpakr

Usage

# alpakr looks for alpakr.yaml or alpakr.yml in the current directory by default
alpakr run                             # uses alpakr.yaml/yml in cwd, defaults to 'root' handler
alpakr run --handler staff             # run a specific handler
alpakr run -c path/to/config.yaml      # explicit config path
alpakr run --format yaml               # override output format (json | yaml | ndjson | csv)
alpakr run -f ndjson | jq -c .         # one record per line
alpakr run -o out.json                 # write to file instead of stdout

alpakr validate                        # check config + compile all expressions
alpakr list-handlers                   # print handler names defined in config

alpakr run | jq .                      # pipe output to other tools
cat data.json | alpakr run             # pipe JSON/YAML via stdin when no source is configured — format auto-detected
cat access.ndjson | alpakr run         # several JSON values / YAML docs on stdin — streamed record by record
jq '.items[]' big.json | alpakr run    # jq output works too (pretty-printed values are fine)
cat other.json | alpakr run --stdin    # read stdin instead of the configured source (format sniffed unless set)
alpakr run --limit 10                  # process only the first 10 records
alpakr run --workers 0                 # process records on all CPUs (default 1); output order is kept

alpakr run --arg region=eu             # jq variable $region (string); --argjson name=<json> for JSON values

alpakr --version                       # print the build version

Config

Single source

version: "1"

source:
  path: ./data/records.json   # local file — mutually exclusive with url:
  # url: https://example.com/data.json
  # format: json              # json | yaml | ndjson | csv — auto-detected from file/URL extension (.jsonl → ndjson, .tsv → csv)
  # delimiter: ","            # csv only (default ","; tab for .tsv)
  # header: true              # csv only: first row names the columns
                              # stdin is read only when no source is configured, or with --stdin
                              # stdin format auto-detected by peeking first byte ({ or [ → json, else yaml)
  # method: POST              # HTTP method (default: GET). POST/PUT/PATCH required when body is set.
  # headers:
  #   Authorization: "Bearer <token>"
  #   Accept: "application/json"
  # body: '{"filter":"active"}'  # request body — requires method POST, PUT, or PATCH
  # timeout: 30s              # max wait for headers / each body read (default 30s; "0" = none)

output:
  format: json                # json | yaml | ndjson (one record per line) | csv — default: json
  indent: 2                   # JSON indentation (default: 2; 0 = compact single line)
  # file: ./out/result.json   # write to file instead of stdout

handlers:
  root:                       # 'root' is used by default if no --handler given
    input: ".data"            # jq selector applied to raw source before processing
    each: true                # iterate array input, run handler per element
    filter: ".active == true" # jq predicate — records that evaluate falsy are dropped
    fields:
      id:    ".id"
      name:  ".name | ascii_upcase"
      score: ".raw_score * 10 | round2"
      tags:  "[.tags[] | ascii_downcase]"
      location:
        handler: place        # delegate this field to another handler
        input: ".loc"         # jq selector to extract input for sub-handler

  place:
    fields:
      city:    ".city"
      country: ".country_code | ascii_upcase"

Strict parsing

Unknown keys are errors, reported with the line, section and valid keys — line 14: unknown key "filtr" in handler (valid keys: source, input, each, filter, fields) — so typos don't silently change behaviour.

Variables

vars: defines values available to every jq expression as $name; --arg name=value (string) and --argjson name=<json> override or add them.

vars:
  region: uk
  min_score: 50

handlers:
  root:
    each: true
    filter: ".score >= $min_score"
    fields:
      label: '"\(.name) (\($region))"'

Environment variables

Source settings (path, url, method, header values, body, timeout), string vars and output.file expand ${VAR} and ${VAR:-default}; an unset ${VAR} without a default is an error, and $$ is a literal $. jq expressions can read $ENV.VAR / env.VAR.

source:
  url: https://${API_HOST:-api.example.com}/v1/items
  headers:
    Authorization: "Bearer ${API_TOKEN}"

Multiple sources

Use a sources map and assign each handler a source: key. Handlers with different sources can coexist in one config file — each alpakr run --handler <name> loads only that handler's source.

version: "1"

sources:
  staff:
    path: ./data/staff.json
  projects:
    path: ./data/projects.yaml   # format auto-detected from extension
  inventory:
    url: https://example.com/inventory.json
    headers:
      Authorization: "Bearer <token>"

output:
  format: json
  indent: 2

handlers:
  staff:
    source: staff
    each: true
    fields:
      id:   ".emp_id"
      name: '.first + " " + .last'

  projects:
    source: projects
    input: ".projects"
    each: true
    filter: '.status == "active"'
    fields:
      id:   ".id"
      name: ".name"

  inventory:
    source: inventory
    each: true
    fields:
      sku:   ".sku"
      stock: ".qty"

When no root handler is defined, --handler is required. The error message lists available handler names. Sub-handlers referenced from fields don't need a source: — they receive data from their parent.

NDJSON (streaming)

Set format: ndjson (auto-detected for .ndjson and .jsonl) to stream newline-delimited JSON files or URL responses without loading the full file into memory. Each line is decoded, passed through the handler, and written to output immediately — suitable for gigabyte-scale log files or event streams.

source:
  path: ./data/access.ndjson
  format: ndjson

handlers:
  root:
    filter: ".status >= 400"   # each line is one record — no 'each: true' needed
    fields:
      time:   '.ts | strptime("%Y-%m-%dT%H:%M:%SZ") | strftime("%d %b %Y %H:%M:%S")'
      method: ".method"
      path:   ".path"
      status: ".status"

With JSON output, each record is written as one compact line (NDJSON) and indent is ignored; with YAML output, records form a ----separated multi-document stream. Pipe via stdin works the same way — cat large.ndjson | alpakr run streams without buffering, and output is flushed whenever alpakr waits for more input, so tail -f app.log | alpakr run --stdin (with a format: ndjson source) emits records as they arrive.

Any JSON or YAML input holding more than one top-level value — NDJSON, concatenated or pretty-printed JSON objects such as jq output, or ----separated YAML documents — is streamed the same way, from a file, URL or stdin. Without format: ndjson, alpakr reads one value ahead to tell a stream from a single document, so the first record is written once the second arrives.

--limit N works with NDJSON sources and stops after N records have been written (filtered-out records don't count toward the limit).

Paginated APIs

pagination: on a URL source follows next-page links and streams the records of every page:

source:
  url: https://api.example.com/v1/items?per_page=100
  headers:
    Authorization: "Bearer ${API_TOKEN}"
  pagination:
    next: ".links.next"   # jq → next URL (absolute or relative) or null to stop
    records: ".data"      # jq → array of records per page (default: the page itself)
    max_pages: 50         # default 1000; 0 = unlimited

Pages are fetched lazily, so --limit stops further requests. Configured headers are only sent to the first URL's origin, never to other hosts a next link names. Expressions also get $response (url, status, lower-cased headers) — e.g. $response.headers.link for Link-header pagination.

CSV

.csv (and tab-separated .tsv) sources stream row by row, like NDJSON: each row is an object keyed by the header, and every value is a string — convert with to_int, to_float or tonumber. -f csv writes one row per record with the header taken from the fields in config order; nested values become compact JSON. See examples/csv.

Field values

Each field value is one of:

  • A jq expression string — evaluated against the current record
  • An inline object — a fields: map of field definitions, evaluated against the same record (or a narrowed sub-selection via input). The explicit fields: key avoids ambiguity with source data that contains keys named handler or input.
  • A sub-handler reference — delegates to a named handler, for shared structures used in multiple places
fields:
  # jq expression
  title: ".name | ascii_downcase"

  # inline nested object — no separate handler needed
  location:
    input: ".loc"   # optional: narrow data before evaluating nested fields
    fields:
      city:    ".city"
      country: ".country_code | ascii_upcase"

  # sub-handler reference
  category:
    handler: cat        # name of handler to run
    input: ".category"  # jq selector to extract input (defaults to .)

Inline objects can nest arbitrarily deep:

fields:
  address:
    fields:
      street: ".street"
      geo:
        fields:
          lat: ".lat"
          lng: ".lng"

Nested data

Handlers compose recursively. A field in one handler can delegate to another handler, which can itself delegate further. This avoids duplicating field mappings for shared structures.

handlers:
  root:
    each: true
    fields:
      title: ".name"
      county:
        handler: county
        input: ".location"

  county:
    fields:
      name: ".county"
      country:
        handler: country
        input: "."

  country:
    fields:
      name: ".country"
      code: ".country_code | ascii_upcase"

Nested collection input

If the source wraps the collection in an object, use input to extract it first:

handlers:
  root:
    input: ".data.records"   # extract the array before each iterates
    each: true
    fields:
      id: ".id"

Built-in transform functions

All standard jq functions are available, plus these extras:

Function Description Example
round2 Round float to 2 decimal places .miles * 1.60934 | round2
slugify Lowercase, spaces→dashes, strip non-alphanumeric .name | slugify
to_int Convert string or float to integer .score | to_int
to_float Convert string or integer to float .count | to_float

Common jq patterns

# String operations
title:     ".name | ascii_downcase"
upper:     ".code | ascii_upcase"
trimmed:   '.label | gsub("^\\s+|\\s+$"; "")'
replaced:  '.text | gsub("foo"; "bar")'

# Math
km:        ".miles * 1.60934 | round2"
remaining: ".budget - .spent"
pct:       "(.spent / .budget * 100) | round2"

# Date formatting
date:      '.iso_date | strptime("%Y-%m-%d") | strftime("%d/%m/%Y")'
from_unix: ".created_ts | todate"

# Arrays
tags:      "[.tags[] | ascii_upcase]"
count:     ".items | length"
total:     "([.lines[] | .qty * .price] | add) | round2"

# Conditionals
type:      'if .kind == "A" then "Alpha" else "Other" end'
street:    '.line1 + (if .line2 != "" then ", " + .line2 else "" end)'

# Null coalescing
label:     '.name // "unknown"'

# Computed / concatenated
full_name: '.first + " " + .last'

Example

Source (data/outings.json):

[
  {
    "id": 1,
    "name": "Peak District Walk",
    "date": "2024-03-15",
    "distance_miles": 8.5,
    "tags": ["hiking", "hills"],
    "location": { "county": "Derbyshire", "country": "England", "country_code": "gb" }
  }
]

Config (alpakr.yaml):

version: "1"

source:
  path: ./data/outings.json

output:
  format: json
  indent: 2

handlers:
  root:
    each: true
    filter: ".distance_miles > 0"
    fields:
      id:          ".id"
      title:       ".name | ascii_downcase"
      date:        '.date | strptime("%Y-%m-%d") | strftime("%d/%m/%Y")'
      distance_km: ".distance_miles * 1.60934 | round2"
      tags:        "[.tags[] | ascii_upcase]"
      location:
        handler: county
        input: ".location"

  county:
    fields:
      name:    ".county"
      slug:    ".county | slugify"
      country:
        handler: country
        input: "."

  country:
    fields:
      name: ".country"
      code: ".country_code | ascii_upcase"

Output (fields appear in config order):

[
  {
    "id": 1,
    "title": "peak district walk",
    "date": "15/03/2024",
    "distance_km": 13.68,
    "tags": [
      "HIKING",
      "HILLS"
    ],
    "location": {
      "name": "Derbyshire",
      "slug": "derbyshire",
      "country": {
        "name": "England",
        "code": "GB"
      }
    }
  }
]

The examples/ directory contains a fully annotated reference config at examples/alpakr.yaml — every option documented with explanation, rules, defaults, and usage examples. Further working examples covering different data shapes and config patterns are in the subdirectories.

Development

make build    # build ./bin/alpakr
make test     # run tests
make test-v   # run tests verbose
make fmt      # gofmt -w
make lint     # gofmt check, go vet, golangci-lint
make tidy     # go mod tidy
make clean    # remove build artifacts

About

YAML-configured data transformation tool.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages