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 9c366b8
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions path_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,62 @@ 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.
items, err := json.Marshal(result)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", items)

// 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.
items, err := json.MarshalIndent(result, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", items)

// Output:
// [
// {
// "node": 19.99,
// "path": "$['apps']['guacamole']"
// },
// {
// "node": 5.99,
// "path": "$['apps']['salsa']"
// }
// ]
}

// 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 9c366b8

Please sign in to comment.