tidwall / sjson

Set JSON values very quickly in Go
MIT License
2.4k stars 165 forks source link

Replace entry => no effect #55

Closed Robert-M-Muench closed 2 years ago

Robert-M-Muench commented 2 years ago

See: https://go.dev/play/p/C3RwPvdGLi1

var json = `["test@test.com","2019-04-01T11:33:39+02:00"]`

res := gjson.Get(json, "[@this].0.0")
fmt.Printf("1: %s\n", res.Raw)

value, _ := sjson.Set(res.Raw, "[@this]", `"*"`)
fmt.Printf("2: %s\n", value)

Why isn't value changed to "*" but still is "test@test.com"?

I want to change the queried JSON part in place.

Robert-M-Muench commented 2 years ago

Trying to use some Path() which doesn't work:

    var json = `["test@test.com","2019-04-01T11:33:39+02:00"]`

    res := gjson.Get(json, "[@this].0.0")
    fmt.Printf("1: %s %s\n", res.Raw, res.Path(json))

Nothing returned for the Path() call.

tidwall commented 2 years ago

An empty string is what Path returns when it cannot determine the original path.

The documentation for Path isn't clear, but the Result must come from a path that does not use modifier, multipath, or nested queries.

I updated the comment:

https://github.com/tidwall/gjson/commit/56c0a0aa5b8ff73f36eab8cd0fa30465cea393d1