Format

Description

Allows you to format dasel output according to the given template.

Usage

Pass the --format flag to select commands.

Available in select commands since v1.18.0.

Functions and accessors

The root context . is equal to the node found at the given selector.

It is recommended that you use the select function with a selector to access values, but you can access properties in the path with .field.subField if preferred.

Dasel also provides access to sprig functions within templates to allow more functionality.

The templates are parsed using golang's text/template package so dasel also supports an array of conditional and loop statements by default.

For more information refer to the related documentation.

Example

Select

$ echo '[
  {"name": "Tom", "email": "contact@tomwright.me"},
  {"name": "Jim", "email": "jim@gmail.com"}
]' | dasel -p json -m \
  --format '{{ select ".name" }},{{ select ".email" }}' \
  '.[*]'
Tom,contact@tomwright.me
Jim,jim@gmail.com

SelectMultiple

$ echo '[
  {"name": "Tom", "emails": [
    {"email": "contact@tomwright.me", "primary": true},
    {"email": "tom@gmail.com", "primary": false}
  ]},
  {"name": "Jim", "emails": [
    {"email": "old@gmail.com", "primary": false},
    {"email": "jim@gmail.com", "primary": true}
  ]}
]' | dasel -p json -m \
  --format '{{ select ".name" }}:{{ newline }}{{ selectMultiple ".emails.[*]" | format "- {{ select \".email\" }}, {{ select \".primary\" }}{{ if not isLast }}{{ newline }}{{ end }}" }}' \
  '.[*]'
Tom:
- contact@tomwright.me, true
- tom@gmail.com, false
Jim:
- old@gmail.com, false
- jim@gmail.com, true

Last updated