Skip to content

Array filter fails if zero results pass the predicate #32

@davesheldon

Description

@davesheldon

Describe the bug
I want to assert that a particular jsonpath evaluates to an empty array. This instead returns jsonpath.ErrorMemberNotExist.

To Reproduce

package main

import (
	"encoding/json"
	"fmt"
	"reflect"

	"github.com/AsaiYusuke/jsonpath"
)

func main() {
	jsonPath, srcJSON := `$[?(@.id == 2)].name`, `[ { "id": 1, "name": "One" } ]`
	var src interface{}
	json.Unmarshal([]byte(srcJSON), &src)
	output, err := jsonpath.Retrieve(jsonPath, src)
	if err != nil {
		fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
		return
	}
	outputJSON, _ := json.Marshal(output)
	fmt.Println(string(outputJSON))
}

This returns: jsonpath.ErrorMemberNotExist, member did not exist (path=[?(@.id == 2)])

Expected behavior
I expected it to return an empty array/slice (e.g. any[]{}).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions