Usage from Go
Introduction
The dasel CLI is a thin wrapper around dasel's Go API. Your application can use the same API to query, transform, and modify structured data.
Installation
go get github.com/tomwright/dasel/v3External API
Dasel exposes three main functions in the root dasel package:
Select- Query data and receive results as native Go types (any).Query- Query data and receive results as*model.Valuetypes (preserves ordering and metadata).Modify- Run a query that modifies the given data in-place.
All three accept variadic execution.ExecuteOptionFn options for features like variables.
Parsing formats
You must import any format parsers you need. The parsers register themselves via init():
import (
_ "github.com/tomwright/dasel/v3/parsing/json"
_ "github.com/tomwright/dasel/v3/parsing/yaml"
_ "github.com/tomwright/dasel/v3/parsing/toml"
_ "github.com/tomwright/dasel/v3/parsing/xml"
_ "github.com/tomwright/dasel/v3/parsing/csv"
_ "github.com/tomwright/dasel/v3/parsing/ini"
_ "github.com/tomwright/dasel/v3/parsing/hcl"
)Only import the formats you actually use.
Examples
Up to date examples are maintained within the GitHub repository under api_example_test.go.
Select: query with native Go types
Use Select when you want results as plain Go values (string, int, map[string]any, etc).
Query: query with model.Value types
Use Query when you need to preserve key ordering or access metadata.
Modify: update data in-place
Use Modify to change values within an existing data structure. The data must be passed as a pointer.
Reading and writing structured data (format conversion)
Use parsers to read bytes in one format and write them in another.
Compact output
Set Compact: true in writer options to produce compact output with no indentation.
Using variables
Pass variables into queries using execution.WithVariable:
Building model.Value manually
You can construct model.Value instances directly without parsing from bytes:
Project structure
Dasel has the following main packages:
dasel - The external API (
Select,Query,Modify).model - A wrapper around reflection types. This is what dasel uses to access and modify data internally.
parsing - Parsing implementations for each supported format. Each subdirectory contains a reader and writer.
execution - The implementation of all dasel features (functions, operators, etc).
selector - Parses dasel query strings and returns an AST used by the
executionpackage.
Last updated