Skip to content

Commit

Permalink
Add Select and SelectLocated examples
Browse files Browse the repository at this point in the history
  • Loading branch information
theory committed Dec 27, 2024
1 parent c055604 commit 6bfe144
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions path_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,51 @@ func Example() {
// Output: ["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]
}

func ExamplePath_Select() {
// Load some JSON.
menu := map[string]any{
"apps": map[string]any{
"guacamole": 19.99,
"salsa": 5.99,
},
}

// Parse a JSONPath and select from the input.
p := jsonpath.MustParse("$.apps.*")
result := p.Select(menu)

// Show the result.
for _, val := range result {
fmt.Printf("%v\n", val)
}
// Unordered output:
// 19.99
// 5.99
}

func ExamplePath_SelectLocated() {
// Load some JSON.
menu := map[string]any{
"apps": map[string]any{
"guacamole": 19.99,
"salsa": 5.99,
},
}

// Parse a JSONPath and select from the input.
p := jsonpath.MustParse("$.apps.*")
result := p.SelectLocated(menu)

// Show the result.
for _, node := range result {
fmt.Printf("%v: %v\n", node.Path, node.Node)
}

// Unordered output:
// $['apps']['guacamole']: 19.99
// $['apps']['salsa']: 5.99
}

// Use the Parser to parse a collection of paths.
func ExampleParser() {
// Create a new parser using the default function registry.
Expand Down

0 comments on commit 6bfe144

Please sign in to comment.