Skip to content

JQ ​

action: jq.filter runs a jq program on JSON and writes each result to stdout. The jq engine is built in, so the host needs no jq binary.

Use it for jq programs that filter, reshape, or aggregate. To pick one value by path or convert CSV and YAML, Data is simpler.

Basic Usage ​

yaml
steps:
  - id: active_emails
    action: jq.filter
    with:
      filter: '.users[] | select(.active) | .email'
      raw: true
      data:
        users:
          - {email: alice@example.com, active: true}
          - {email: bob@example.com, active: false}
          - {email: carol@example.com, active: true}

stdout:

text
alice@example.com
carol@example.com

Each result goes on its own line. Without raw: true, results are JSON, indented with four spaces, so these would print with quotes.

Provide the Input ​

Set one of data or input:

FormExampleReads
YAML valuedata: {items: [1, 5, 10]}The value, as JSON.
JSON stringdata: '{"name": "Alice"}'The string, parsed as JSON.
file:// stringdata: file://report.jsonThe file, parsed as JSON.
inputinput: report.jsonThe file, parsed as JSON.

A relative input or file:// path resolves against the step working directory, which is a fresh per-run directory unless working_dir is set. To filter a file an earlier step wrote, pass its path through an output:

yaml
steps:
  - id: export
    run: |
      path="$DAG_RUN_WORK_DIR/items.json"
      printf '%s\n' '{"items": [{"name": "a"}, {"name": "b"}]}' > "$path"
      printf 'json_path=%s\n' "$path" >> "$DAGU_OUTPUT_FILE"
    outputs:
      - name: json_path

  - id: names
    action: jq.filter
    with:
      input: ${steps.export.outputs.json_path}
      filter: '.items[].name'
      raw: true

Pass Values Into the Filter ​

args binds jq variables. Each key becomes $key in the filter, and values keep their YAML type:

yaml
params:
  - min_price: "4"

steps:
  - id: cheap_fruit
    action: jq.filter
    with:
      filter: '.items[] | select(.price > ($min | tonumber)) | .name'
      raw: true
      args:
        min: ${params.min_price}
      data:
        items:
          - {name: apple, price: 5}
          - {name: pear, price: 3}

stdout: apple.

  • A reference such as ${params.min_price} resolves to a string. Convert it with tonumber before comparing it to a JSON number. A literal min: 4 stays a number.
  • A key may start with one $: min and $min bind the same variable, and giving both fails.
  • To pass a literal $NAME, escape it: text: '\$NAME'.

Keep the Filter Literal ​

Without args, Dagu resolves references in the filter before jq runs, including environment variables written as $NAME. A filter of '"hello $NAME"' with NAME: world in the DAG env prints "hello world".

With args, even args: {}, the whole filter is passed to jq unchanged. Use args whenever the filter has jq variables of its own, and pass workflow values through it.

Reshape and Aggregate ​

yaml
steps:
  - id: sales_by_category
    action: jq.filter
    with:
      filter: |
        group_by(.category)
        | map({category: .[0].category, total: (map(.amount) | add), count: length})
      data:
        - {category: Electronics, amount: 299.99}
        - {category: Clothing, amount: 49.99}
        - {category: Electronics, amount: 199.99}

The step prints one JSON array. Capture it with output: NAME, or publish fields from it with object-form output.

Configuration ​

FieldRequiredDefaultDescription
filterYesjq program.
dataOne of data, inputInline YAML value, JSON string, or file:// path.
inputOne of data, inputPath to a JSON file.
rawNofalseWrite strings without quotes, like jq -r.
argsNoNoneMap of jq variable name to value. Its presence keeps the filter literal.

Output ​

  • Without raw: each result as JSON, indented with four spaces, followed by a newline.
  • With raw: strings without quotes, numbers and booleans as text, and null as an empty line. Objects and arrays are written as compact JSON.
  • halt stops the program and the step succeeds. halt_error fails the step.

Errors ​

Validation rejects a missing filter, both data and input, an empty data string, and an unknown field.

These fail when the step runs:

  • Neither data nor input is set.
  • The input file cannot be read, or the input is not valid JSON.
  • The filter does not parse or compile, including a variable that args does not define.
  • args binds the same variable twice, such as min and $min.
  • The filter raises an error while it runs, such as indexing a string.

Dagu is open source under the GNU General Public License v3.0.