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.
​
Function
Description
select "selector"
Returns the node at the given selector.
selectMultiple "selector"
Returns a list of nodes found for the given selector.
query
Alias of select.
queryMultiple
Alias of selectMultiple.
isFirst
Returns true if the node being formatted is the first in a list of selected nodes.
isLast
Returns true if the node being formatted is the last in a list of selected nodes.
format "template"
Allows recursive calls to the formatting capability. Useful when using a selectMultiple.
newline
Returns a newline character.
The templates are parsed using golang's text/template package so dasel also supports an array of conditional and loop statements by default.
Description
Example
If condition
{{ if x }} x is true {{ else }} x is false {{ end }}
If not condition
{{ if not x }} x is false {{ else }} x is true {{ end }}
Range
Numbers:
{{ range .numbers -}} - {{ . }}
{{ end }}
Text and space manipulation
For more information refer to the related documentation.

Example

Select

1
echo '[
2
{"name": "Tom", "email": "[email protected]"},
3
{"name": "Jim", "email": "[email protected]"}
4
]' | dasel -p json -m \
5
--format '{{ select ".name" }},{{ select ".email" }}' \
6
'.[*]'
Copied!

SelectMultiple

1
echo '[
2
{"name": "Tom", "emails": [
3
{"email": "[email protected]", "primary": true},
4
{"email": "[email protected]", "primary": false}
5
]},
6
{"name": "Jim", "emails": [
7
{"email": "[email protected]", "primary": false},
8
{"email": "[email protected]", "primary": true}
9
]}
10
]' | dasel -p json -m \
11
--format '{{ select ".name" }}:{{ newline }}{{ selectMultiple ".emails.[*]" | format "- {{ select \".email\" }}, {{ select \".primary\" }}{{ if not isLast }}{{ newline }}{{ end }}" }}' \
12
'.[*]'
13
Tom:
16
Jim:
Copied!
Last modified 2mo ago