Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions jsonpath/jsonpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ func walk(path string, object interface{}) (string, int, error) {
}
return value, len(value), nil
case []interface{}:
// Wildcard array support
if strings.HasSuffix(currentKey, "[*]") {
remainingPath := strings.Replace(path, fmt.Sprintf("%s.", currentKey), "", 1)
var results []string
for _, item := range value {
result, _, err := walk(remainingPath, item)
if err != nil {
return "", 0, err
}
results = append(results, result)
}
finalResult := strings.Join(results, ", ")
return finalResult, len(finalResult), nil
}
// Normal arrays (backward compatibility)
return fmt.Sprintf("%v", value), len(value), nil
case interface{}:
newValue := fmt.Sprintf("%v", value)
Expand All @@ -67,6 +82,20 @@ func walk(path string, object interface{}) (string, int, error) {
}

func extractValue(currentKey string, value interface{}) interface{} {
// Wildcard array support: data[*]
if strings.HasSuffix(currentKey, "[*]") {
baseKey := currentKey[:len(currentKey)-3]
var array []interface{}
if len(baseKey) == 0 {
// if the baseKey is empty, then we are at the root of the JSON object
array, _ = value.([]interface{})
} else {
if valueAsMap, ok := value.(map[string]interface{}); ok {
array, _ = valueAsMap[baseKey].([]interface{})
}
}
return array
}
// Check if the current key ends with [#]
if strings.HasSuffix(currentKey, "]") && strings.Contains(currentKey, "[") {
var isNestedArray bool
Expand Down
32 changes: 32 additions & 0 deletions jsonpath/jsonpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,38 @@ func TestEval(t *testing.T) {
ExpectedOutputLength: 0,
ExpectedError: true,
},
{
Name: "object-with-arrays-of-objects-use-wildcard-in-root-single",
Path: "apps[*].name",
Data: `{"apps": [{"name": "app1","value": "value1"}]}`,
ExpectedOutput: "app1",
ExpectedOutputLength: 4,
ExpectedError: false,
},
{
Name: "object-with-arrays-of-objects-use-wildcard-in-root-multiple",
Path: "apps[*].name",
Data: `{"apps": [{"name": "app1","value": "value1"},{"name": "app2","value": "value2"},{"name": "app3","value": "value3"}]}`,
ExpectedOutput: "app1, app2, app3",
ExpectedOutputLength: 16,
ExpectedError: false,
},
{
Name: "object-with-arrays-of-objects-use-wildcard-not-in-root-single",
Path: "data[0].apps[*].name",
Data: `{"data": [{"apps": [{"name": "app1","value": "value1"}]}]}`,
ExpectedOutput: "app1",
ExpectedOutputLength: 4,
ExpectedError: false,
},
{
Name: "object-with-arrays-of-objects-use-wildcard-not-in-root-multiple",
Path: "data[0].apps[*].name",
Data: `{"data": [{"apps": [{"name": "app1","value": "value1"},{"name": "app2","value": "value2"},{"name": "app3","value": "value3"}]}]}`,
ExpectedOutput: "app1, app2, app3",
ExpectedOutputLength: 16,
ExpectedError: false,
},
{
Name: "partially-invalid-path-issue122",
Path: "data.name.invalid",
Expand Down