### Install Dasel using Go Install Source: https://github.com/tomwright/dasel/blob/master/README.md Installs the Dasel command-line tool using the Go install command. This method requires Go to be installed on your system and fetches the latest version from the master branch. ```bash go install github.com/tomwright/dasel/v3/cmd/dasel@master ``` -------------------------------- ### Install Dasel using Homebrew Source: https://github.com/tomwright/dasel/blob/master/README.md Installs the Dasel command-line tool using the Homebrew package manager. This is a convenient method for users on macOS and Linux systems that have Homebrew installed. ```bash brew install dasel ``` -------------------------------- ### Find Maximum and Minimum Values (Bash) Source: https://context7.com/tomwright/dasel/llms.txt Illustrates the `max` and `min` functions in Dasel for determining the largest or smallest value among a set of arguments. Examples are provided for finding extremes in object property values. ```bash # Maximum value echo '{"a": 10, "b": 25, "c": 15}' | dasel -i json 'max(a, b, c)' # Output: 25 # Minimum value echo '{"a": 10, "b": 25, "c": 15}' | dasel -i json 'min(a, b, c)' # Output: 10 ``` -------------------------------- ### Sum Numeric Values (Bash) Source: https://context7.com/tomwright/dasel/llms.txt Shows how to use the `sum` function in Dasel to calculate the total of numeric values from arrays or object properties. Examples cover both array elements and object values. ```bash echo '[1, 2, 3, 4, 5]' | dasel -i json 'sum($this[0], $this[1], $this[2], $this[3], $this[4])' # Output: 15 echo '{"a": 10, "b": 20}' | dasel -i json 'sum(a, b)' # Output: 30 ``` -------------------------------- ### Base64 Encoding and Decoding (Bash) Source: https://context7.com/tomwright/dasel/llms.txt Demonstrates Dasel's `base64e` (encode) and `base64d` (decode) functions for handling Base64 data. Examples show encoding a string and decoding a Base64 encoded string. ```bash # Encode to base64 echo '"hello world"' | dasel -i json 'base64e($this)' # Output: "aGVsbG8gd29ybGQ=" # Decode from base64 echo '"aGVsbG8gd29ybGQ="' | dasel -i json 'base64d($this)' # Output: "hello world" ``` -------------------------------- ### Check for Existence of Key or Index (Bash) Source: https://context7.com/tomwright/dasel/llms.txt Illustrates the `has` function in Dasel for verifying the presence of a key in a map or an index in an array. Examples show checks for both existing and non-existing elements. ```bash # Check map key echo '{"name": "Alice"}' | dasel -i json 'has($this, "name")' # Output: true echo '{"name": "Alice"}' | dasel -i json 'has($this, "age")' # Output: false # Check array index echo '[1, 2, 3]' | dasel -i json 'has($this, 1)' # Output: true ``` -------------------------------- ### Type Conversion Functions (Bash) Source: https://context7.com/tomwright/dasel/llms.txt Illustrates Dasel's type conversion functions: `toString`, `toInt`, and `toFloat`. Examples show converting numbers to strings and string representations of numbers to their numeric types. ```bash # Convert to string echo '42' | dasel -i json 'toString($this)' # Output: "42" # Convert to integer echo '"123"' | dasel -i json 'toInt($this)' # Output: 123 # Convert to float echo '"3.14159"' | dasel -i json 'toFloat($this)' # Output: 3.14159 ``` -------------------------------- ### Concatenate Strings with Separator (Bash) Source: https://context7.com/tomwright/dasel/llms.txt Demonstrates the `join` function in Dasel for combining array elements or arguments into a single string, optionally using a specified separator. Examples show joining array elements and custom separators. ```bash # Join array elements echo '["hello", "world"]' | dasel -i json 'join(" ", $this)' # Output: "hello world" # Join with custom separator echo '["a", "b", "c"]' | dasel -i json 'join("-", $this)' # Output: "a-b-c" ``` -------------------------------- ### Get Length of Data Structures (Bash) Source: https://context7.com/tomwright/dasel/llms.txt Demonstrates the `len` function in Dasel for retrieving the number of elements in arrays, strings, or maps. Examples show its usage with JSON input via the command line. ```bash # Array length echo '["a", "b", "c"]' | dasel -i json 'len($this)' # Output: 3 # String length echo '"hello world"' | dasel -i json 'len($this)' # Output: 11 ``` -------------------------------- ### Reverse Order of Strings or Arrays (Bash) Source: https://context7.com/tomwright/dasel/llms.txt Demonstrates the `reverse` function in Dasel for reversing the order of elements in an array or characters in a string. Examples cover both data types. ```bash # Reverse array echo '[1, 2, 3, 4, 5]' | dasel -i json 'reverse($this)' # Output: [5, 4, 3, 2, 1] # Reverse string echo '"hello"' | dasel -i json 'reverse($this)' # Output: "olleh" ``` -------------------------------- ### Replace Substrings in a String (Bash) Source: https://context7.com/tomwright/dasel/llms.txt Explains the `replace` function in Dasel for substituting occurrences of a substring within a string. Examples show single and multiple replacements. ```bash echo '"hello world"' | dasel -i json 'replace($this, "world", "dasel")' # Output: "hello dasel" # Multiple replacements echo '"foo bar baz"' | dasel -i json 'replace($this, "foo", "1", "bar", "2")' # Output: "1 2 baz" ``` -------------------------------- ### Recursive Descent Search in JSON using Dasel CLI Source: https://github.com/tomwright/dasel/blob/master/README.md Explains and demonstrates the recursive descent operator (`..`) in Dasel CLI for searching nested structures. This example finds all values associated with the key 'bar' within a JSON object. ```bash echo '{"foo": {"bar": "baz"}}' | dasel -i json '..bar' ``` -------------------------------- ### Search for Values Matching a Condition in JSON using Dasel CLI Source: https://github.com/tomwright/dasel/blob/master/README.md Illustrates the `search` function in Dasel CLI for finding elements that satisfy a specific condition. This example searches for objects where the 'bar' key equals 'baz' within a JSON structure. ```bash echo '{"foo": {"bar": "baz"}}' | dasel -i json 'search(bar == "baz")' ``` -------------------------------- ### Check if Array Contains Value (Bash) Source: https://context7.com/tomwright/dasel/llms.txt Shows the `contains` function in Dasel for determining if an array includes a specific value. Examples demonstrate checking for existing and non-existing values within an array. ```bash echo '["apple", "banana", "cherry"]' | dasel -i json 'contains($this, "banana")' # Output: true echo '[1, 2, 3, 4, 5]' | dasel -i json 'contains($this, 99)' # Output: false ``` -------------------------------- ### Merge Multiple Map Objects (Bash) Source: https://context7.com/tomwright/dasel/llms.txt Demonstrates the `merge` function in Dasel for combining multiple map objects into a single map. Examples show merging two objects and merging several objects into an empty one. ```bash echo '{"a": 1}' | dasel -i json 'merge($this, {"b": 2})' # Output: {"a": 1, "b": 2} echo '{}' | dasel -i json 'merge($this, {"x": 1}, {"y": 2}, {"z": 3})' # Output: {"x": 1, "y": 2, "z": 3} ``` -------------------------------- ### Perform Basic Queries and Format Conversions with Dasel CLI Source: https://context7.com/tomwright/dasel/llms.txt Demonstrates how to use the dasel command to query properties, access array elements, and convert between data formats like JSON and YAML. It accepts input via stdin and uses flags to define input/output formats. ```bash # Basic property access echo '{"name": "Alice", "age": 30}' | dasel -i json 'name' # Nested property access with dot notation echo '{"user": {"profile": {"email": "alice@example.com"}}}' | dasel -i json 'user.profile.email' # Array index access echo '["a", "b", "c", "d"]' | dasel -i json '$this[1]' # Format conversion (JSON to YAML) echo '{"name": "Alice", "age": 30}' | dasel -i json -o yaml # YAML to JSON conversion echo -e "name: Alice\nage: 30" | dasel -i yaml -o json ``` -------------------------------- ### Configure Query Execution with Variables and Unstable Features (Go) Source: https://context7.com/tomwright/dasel/llms.txt Demonstrates how to pass variables and enable unstable features when executing queries using the Dasel Go API. It shows how to use `execution.WithVariable` to inject values and `execution.WithUnstable` for features like filtering. ```go package main import ( "context" "fmt" "github.com/tomwright/dasel/v3" "github.com/tomwright/dasel/v3/execution" "github.com/tomwright/dasel/v3/model" ) func main() { data := map[string]any{ "items": []any{10, 20, 30, 40, 50}, } // Pass a variable to the query threshold := model.NewValue(25) result, count, err := dasel.Select( context.Background(), data, "items.filter($this > $threshold)...", execution.WithVariable("threshold", threshold), execution.WithUnstable(), // Required for filter ) if err != nil { panic(err) } fmt.Printf("Found %d items above threshold:\n", count) fmt.Println(result) // Output: // Found 3 items above threshold: // [30 40 50] } ``` -------------------------------- ### Low-Level Selector Execution with Dasel API (Go) Source: https://context7.com/tomwright/dasel/llms.txt Illustrates the direct use of the `ExecuteSelector` function from the Dasel Go API for granular control over query execution. It shows how to create `model.Value` from data, configure execution options including unstable features, and retrieve results. ```go package main import ( "context" "fmt" "github.com/tomwright/dasel/v3/execution" "github.com/tomwright/dasel/v3/model" ) func main() { // Create a model.Value from raw data data := model.NewValue(map[string]any{ "config": map[string]any{ "debug": true, "timeout": 30, }, }) // Create execution options options := execution.NewOptions( execution.WithUnstable(), ) // Execute selector directly result, err := execution.ExecuteSelector( context.Background(), "config.timeout", data, options, ) if err != nil { panic(err) } // Access the result value intVal, _ := result.IntValue() fmt.Printf("Timeout: %d\n", intVal) // Output: Timeout: 30 } ``` -------------------------------- ### Go: Select Data with dasel.Select Source: https://context7.com/tomwright/dasel/llms.txt Shows how to use the `dasel.Select` function in Go to execute a query and convert results directly to native Go types. This is useful for integrating Dasel results into existing Go code. ```go package main import ( "context" "fmt" "github.com/tomwright/dasel/v3" "github.com/tomwright/dasel/v3/execution" ) func main() { myData := map[string]any{ "users": []map[string]any{ {"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}, {"name": "Tom", "age": 40}, }, } // Filter users over 27 and extract names query := `users.filter(age > 27).map(name)...` // WithUnstable() enables experimental features like filter selectResult, numResults, err := dasel.Select( context.Background(), myData, query, execution.WithUnstable(), ) if err != nil { panic(err) } fmt.Printf("Found %d results:\n", numResults) // Results are native Go types for _, result := range selectResult.([]any) { fmt.Println(result) } // Output: // Found 2 results: // Alice // Tom } ``` -------------------------------- ### Go: Query Data with dasel.Query Source: https://context7.com/tomwright/dasel/llms.txt Demonstrates using the `dasel.Query` function in Go to execute a selector against data. This function returns `*model.Value` results, offering detailed access to the internal data representation. ```go package main import ( "context" "fmt" "github.com/tomwright/dasel/v3" ) func main() { inputData := map[string]any{ "users": []map[string]any{ {"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}, {"name": "Tom", "age": 40}, }, } // Query returns []*model.Value which provides rich type access results, count, err := dasel.Query(context.Background(), inputData, "users.map(name)...") if err != nil { panic(err) } fmt.Printf("Found %d results:\n", count) for _, r := range results { strVal, _ := r.StringValue() fmt.Println(strVal) } // Output: // Found 3 results: // Alice // Bob // Tom } ``` -------------------------------- ### Map Over Arrays with Dasel CLI Source: https://context7.com/tomwright/dasel/llms.txt Demonstrates the map() expression to transform array elements, extract specific fields, or restructure objects within an array. ```bash # Extract single field from each object echo '[{"name":"Alice","age":30},{"name":"Bob","age":25}]' | dasel -i json 'map(name)...' # Extract nested property echo '[{"user":{"email":"a@example.com"}},{"user":{"email":"b@example.com"}}]' | dasel -i json 'map(user.email)...' # Create new object structure echo '[{"first":"Alice","last":"Smith"},{"first":"Bob","last":"Jones"}]' | dasel -i json 'map({fullName: first})...' ``` -------------------------------- ### Convert JSON to YAML using Dasel CLI Source: https://github.com/tomwright/dasel/blob/master/README.md Shows how to convert data from JSON format to YAML format using the Dasel CLI. It reads from standard input (e.g., a file `data.json`) and specifies the output format as YAML. ```bash cat data.json | dasel -i json -o yaml ``` -------------------------------- ### Using Variables in Dasel Queries Source: https://context7.com/tomwright/dasel/llms.txt Explains how to pass variables into Dasel queries using the `--var` flag for dynamic data injection. Variables can be strings, numbers, or even JSON structures. ```bash # String variable echo '{"threshold": 50}' | dasel -i json --var target=75 'threshold = toInt($target)' # Output: 75 ``` ```bash # Use in conditions echo '[1, 2, 3, 4, 5]' | dasel -i json --var limit=3 'filter($this <= toInt($limit))...' # Output: [1, 2, 3] ``` ```bash # JSON variable from file echo '{}' | dasel -i json --root --var data='json:{"name":"test"}' 'merge($this, $data)' ``` -------------------------------- ### Select Values from JSON using Dasel CLI Source: https://github.com/tomwright/dasel/blob/master/README.md Demonstrates how to select a specific value from a JSON string using the Dasel command-line interface. It pipes a JSON object to dasel, specifying the input format as JSON and providing a dot-notation path to the desired value. ```bash echo '{"foo": {"bar": "baz"}}' | dasel -i json 'foo.bar' ``` -------------------------------- ### Go: Modify Data In-Place with dasel.Modify Source: https://context7.com/tomwright/dasel/llms.txt Demonstrates using the `dasel.Modify` function in Go to update values directly within a data structure. The data parameter must be a pointer to enable in-place modification. ```go package main import ( "context" "fmt" "github.com/tomwright/dasel/v3" ) func main() { // Data must be a pointer for in-place modification data := []any{1, 2, 3, 4, 5} // Modify the second element (index 1) count, err := dasel.Modify(context.Background(), &data, "$this[1]", 99) if err != nil { panic(err) } fmt.Printf("Modified %d value(s)\n", count) fmt.Println(data) // Output: // Modified 1 value(s) // [1 99 3 4 5] // Modify nested map value mapData := map[string]any{ "user": map[string]any{ "name": "Alice", "status": "active", }, } _, err = dasel.Modify(context.Background(), &mapData, "user.status", "inactive") if err != nil { panic(err) } fmt.Println(mapData) // Output: map[user:map[name:Alice status:inactive]] } ``` -------------------------------- ### Modify and Output Full JSON Document using Dasel CLI Source: https://github.com/tomwright/dasel/blob/master/README.md Illustrates how to modify a value in a JSON structure and output the entire modified document using the `--root` flag with Dasel CLI. This is useful for seeing the complete structure after an update. ```bash echo '{"foo": {"bar": "baz"}}' | dasel -i json --root 'foo.bar = "bong"' ``` -------------------------------- ### Search Data with Conditions using Dasel CLI Source: https://context7.com/tomwright/dasel/llms.txt Uses the search() function to find all values or objects that satisfy a specific condition across the entire data structure. ```bash # Search for objects matching condition echo '{"foo":{"bar":"baz"},"other":{"bar":"different"}}' | dasel -i json 'search(bar == "baz")' # Search for numeric conditions echo '{"items":[{"val":10},{"val":20},{"val":30}]}' | dasel -i json 'search(val > 15)' ``` -------------------------------- ### Update Array Elements in JSON using Dasel CLI Source: https://github.com/tomwright/dasel/blob/master/README.md Demonstrates updating all elements in a JSON array by doubling their values using the `each` function and `$this` variable within Dasel CLI. The `--root` flag ensures the entire modified array is output. ```bash echo '[1,2,3,4,5]' | dasel -i json --root 'each($this = $this*2)' ``` -------------------------------- ### Modify Data In-Place with Dasel CLI Source: https://context7.com/tomwright/dasel/llms.txt Shows how to use the assignment operator to update values within structured data. The --root flag is used to output the entire modified document instead of just the updated value. ```bash # Modify a single value echo '{"name": "Alice", "status": "active"}' | dasel -i json 'status = "inactive"' # Modify and return full document with --root echo '{"user": {"name": "Alice"}}' | dasel -i json --root 'user.name = "Bob"' # Modify array element echo '[1, 2, 3]' | dasel -i json --root '$this[1] = 99' # Update values based on current value using $this echo '[1, 2, 3, 4, 5]' | dasel -i json --root 'each($this = $this * 2)' ``` -------------------------------- ### Perform Recursive Descent Searches with Dasel CLI Source: https://context7.com/tomwright/dasel/llms.txt Uses the .. operator to perform a recursive search through nested data structures to locate specific keys or values regardless of depth. ```bash # Find all values for a key anywhere in the structure echo '{"a":{"name":"Alice"},"b":{"c":{"name":"Bob"}}}' | dasel -i json '..name' # Search deeply nested data echo '{"level1":{"level2":{"target":"found"},"other":"ignore"}}' | dasel -i json '..target' # Combine with other operations echo '{"users":[{"profile":{"id":1}},{"profile":{"id":2}}]}' | dasel -i json '..id' ``` -------------------------------- ### Iterate and Modify Array Elements with Dasel Source: https://context7.com/tomwright/dasel/llms.txt Illustrates using the `each()` expression in Dasel to iterate over array elements and apply transformations. This can be used to modify individual values or nested fields within each element. ```bash # Multiply each value echo '[1, 2, 3, 4]' | dasel -i json --root 'each($this = $this * 10)' # Output: [10, 20, 30, 40] ``` ```bash # Update nested field in each object echo '[{"score":50},{"score":75}]' | dasel -i json --root 'each(score = score + 10)' # Output: [{"score":60},{"score":85}] ``` -------------------------------- ### Sort JSON Array by String Field Source: https://context7.com/tomwright/dasel/llms.txt Shows how to sort a JSON array of objects alphabetically based on a string field. The `sortBy` function handles string comparisons correctly. ```bash echo '[{"name":"Charlie"},{"name":"Alice"},{"name":"Bob"}]' | dasel -i json 'sortBy(name)...' # Output: [{"name":"Alice"},{"name":"Bob"},{"name":"Charlie"}] ``` -------------------------------- ### Modify Values in JSON using Dasel CLI Source: https://github.com/tomwright/dasel/blob/master/README.md Shows how to modify a value within a JSON structure using the Dasel CLI. The command updates the 'bar' field to 'bong' and prints the modified value. The input format is specified as JSON. ```bash echo '{"foo": {"bar": "baz"}}' | dasel -i json 'foo.bar = "bong"' ``` -------------------------------- ### Sort JSON Array by Numeric Field (Ascending/Descending) Source: https://context7.com/tomwright/dasel/llms.txt Demonstrates sorting a JSON array of objects based on a numeric field. The default sort order is ascending, but descending order can be specified. ```bash echo '[{"name":"C","score":30},{"name":"A","score":10},{"name":"B","score":20}]' | dasel -i json 'sortBy(score)...' # Output: [{"name":"A","score":10},{"name":"B","score":20},{"name":"C","score":30}] ``` ```bash echo '[{"name":"A","val":1},{"name":"B","val":3},{"name":"C","val":2}]' | dasel -i json 'sortBy(val, desc)...' # Output: [{"name":"B","val":3},{"name":"C","val":2},{"name":"A","val":1}] ``` -------------------------------- ### Filter Arrays using Dasel CLI Source: https://context7.com/tomwright/dasel/llms.txt Utilizes the filter() expression to extract specific elements from an array based on conditional logic. Supports chaining with other operations like map(). ```bash # Filter objects by field value echo '[{"name":"Alice","age":30},{"name":"Bob","age":25},{"name":"Tom","age":40}]' | dasel -i json 'filter(age > 27)' # Filter with equality check echo '[{"type":"error","msg":"fail"},{"type":"info","msg":"ok"}]' | dasel -i json 'filter(type == "error")' # Chain filter with map to extract specific fields echo '[{"name":"Alice","age":30},{"name":"Bob","age":25}]' | dasel -i json 'filter(age >= 30).map(name)...' ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.