ohler55 / ojg

Optimized JSON for Go
MIT License
837 stars 50 forks source link

Cannot parse when properties start with "$" #167

Closed Skeeve closed 4 months ago

Skeeve commented 6 months ago

I'm still working on my templating app and found another issue.

In order to "mark" my template function I start their names with a "$". Unfortunately your framework cannot parse json path descriptions, where a property starts with "$" like $..$printf. But this seems to be a valid path as tested with

https://www.site24x7.com/de/tools/json-path-evaluator.html and https://jsonpath.com

Also see https://swagger.io/docs/specification/using-ref/ for real-world-usage of such properties.

Please find below example on the Playground.

Update: Also a path like $..[printf] cannot be parsed.

Update 2: Also this fails: an expression fragment can not start with a '$' at 10 in $..[?(@.$printf)]

Update 3: Maybe close this as I had a look onto the IETF draft and found that one has to use the index selector.

Checking with your jp reveals that $..['$printf'] works.

package main

import (
    "fmt"

    "github.com/ohler55/ojg/jp"
    "github.com/ohler55/ojg/oj"
)

func main() {
    data, err := oj.ParseString(`
    {
        "type": "AdaptiveCard",
        "body": [
            {
                "type": "Container",
                "style": "attention",
                "isVisible": "$._has_content.fail",
                "items": [
                    {
                        "type": "TextBlock",
                        "text": {
                "$printf": [
                    "test%s", "text" ] }
                    },
                    {
                        "type": "FactSet",
                        "facts": [
                            "$.fail.*"
                        ]
                    }
                ]
            }
        ]
    }
    `)
    if err != nil {
        fmt.Println(err.Error())
        return
    }

    x, err := jp.ParseString(`$..$printf`)
    if err != nil {
        fmt.Println(err.Error())
        return
    }
    fmt.Println(x.String())
    result := x.Get(data)
    fmt.Println(oj.JSON(result))
}
ohler55 commented 4 months ago

Seems like the issue has been resolved. If you wanted to add this to a discussion that might help someone else in the future with a similar issue.