Closed BorisKozo closed 4 years ago
If the path points to a unique entry (it doesn't contain *
, ..
etc) it returns directly the requested value. This was done in order to support expressions like $.a + $.b
I have to agree with @BorisKozo, this causes a consistency issue. I'll look through the code to see if I can find the things that indicate it's a unique entry and update code to turn []any
into [][]any
. If I find something and write the code, I'll share it here.
@generikvault Would you be willing to help me come up with a way to parse/process a JSONPath string and tell when PaesslerAG/jsonpath
has removed an outer slice? I'll do my best to do so but if you're willing, it could help us all out.
The quick and dirty is as simple as the following, but I'm worried it might not be this simple:
func wrapSingleValue(path string, value []any) []any {
if !strings.Contains(path, "..") && !strings.Contains(path, "*") {
return []any{value}
}
return value
}
If anyone can shed light on this, I'd appreciate it.
@whitlockjc this happens in path.go. when the path interface has the dynamic type plainPath a scalar value is returned otherwise the dynamic type is ambiguousPath and a slice is returned.
You can not really distinguish these two without parsing the path since paths like $["a", "b"]
are also ambiguous.
It is probably the easiest to add a gval.ConsistantReturnLanguage
.
But is $["a", "b"]
valid JSONPath? I do not think that it is, no other JSONPath implementations seem to support it. If not, then the use cases above that support the need to deviate from the JSONPath "standard (quoted since it's not really a standard)" do not need to be supported for my tool and the naive approach above where you check the path for .., *, ?
might be a decent approach. To test, I used multiple other JSONPath implementations to verify and it seems to work.
By the way, I appreciate you taking the time to explain/help.
For arrays it is valid: $.aList[1,4,9]
For objects I've seen different implementation.
Arrays hav also a range notation $.aList[0:3]
I found an issue with the return value of a query returning an array. If I query for the array element itself it returns the array instead of an array that contains the value (which is also an array).
Example (based on https://jsonpath.com/) JSON:
Query:
$.phoneNumbers[0:1]
returns (correctly):Query:
$.phoneNumbers
returns:instead of (see the outer array):
The worst part is that I have no way of knowing if I am expecting a single value or multiple values just by querying the returned type and without parsing the query itself.
Thanks.