PaesslerAG / jsonpath

BSD 3-Clause "New" or "Revised" License
172 stars 37 forks source link

Support for [(@.length-1)] #34

Open Zatte opened 1 year ago

Zatte commented 1 year ago

Hi,

How would one go about to get support for the [@.length - n] syntax illustrated @ https://goessner.net/articles/JsonPath/

I know that [start:stop] using negative indices is an ok workaround if we have control over the input. Given that gval and jsonpath is meant for end-users to input business logic it would be nice to expose full jsonpath engine and not jsonpath minus some features.

    v := interface{}(nil)
    json.Unmarshal([]byte(`{
        "welcome":{
                "message":["Good Morning", "Hello World!"]
            }
        }`), &v)
    welcome, err := jsonpath.Get("$.welcome.message[(@.length -1)]", v)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    fmt.Println(welcome)

https://go.dev/play/p/tDVmCrg6_OJ

=>

parsing error: $.welcome.message[(@.length -1)] :1:29 - 1:30 unexpected "-" while scanning parentheses expected ")"

PS. Awesome work with both gval and jsonpath; Amazing libraries!