New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Question] Any way to find the original full property path that got matched? #206
Comments
Sorry, but there isn't a built in way to get the full path back. |
It could be useful to replace found values via sjson lib |
+1, I would like to see this feature implemented. I actually tried adding it myself but found the code way too hard to navigate. It also seems like this isn't possible right now, since array matches are stored as Raw strings and rebuilt during the call to arrayOrMap (I think), so there isn't enough information to rebuild the path, i.e. at which index it matched and with which sub-key. |
This is a feature that I believe would be pretty awesome. The main hurdle is that gjson, in most common cases, does not allocate any new memory during a Determining the original path from a The final A while back I included the I think these two fields can be used to generate the original path by performing a fast reverse parse from the position of the Such as from the OP: const json = `
{
"name": {"first": "Tom", "last": "Anderson"},
"age":37,
"children": ["Sara","Alex","Jack"],
"fav.movie": "Deer Hunter",
"friends": [
{"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]},
{"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]},
{"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]}
]
}
`
value := gjson.Get(json, "friends.#.first")
fmt.Println(value.Array()[0].Path(json))
// Output:
// friends.0.first |
This commit adds a two new functions of the Result type: - Result.Path: Returns the original path of a `Result` that was returned from a simple `Get` operation. - Result.Paths: Returns the original paths of a `Result` that was returned from a `Get` operation with a query. See issue #206 for more details
I just pushed an update that includes this feature. const json = `
{
"name": {"first": "Tom", "last": "Anderson"},
"age":37,
"children": ["Sara","Alex","Jack"],
"fav.movie": "Deer Hunter",
"friends": [
{"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]},
{"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]},
{"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]}
]
}
`
value := Get(readmeJSON, "friends.#.first")
fmt.Println(value.Array()[0].Path(readmeJSON))
value = Get(readmeJSON, "friends.#.first")
fmt.Println(value.Paths(readmeJSON))
value = Get(readmeJSON, "friends.#(last=Murphy)")
fmt.Println(value.Path(readmeJSON))
value = Get(readmeJSON, "friends.#(last=Murphy)#")
fmt.Println(value.Paths(readmeJSON))
// Output:
// friends.0.first
// [friends.0.first friends.1.first friends.2.first]
// friends.0
// [friends.0 friends.2] |
For eg (Pseudocode. Does not work)
The usefulness of this is when we're specifying wildcard matches
The text was updated successfully, but these errors were encountered: