PaesslerAG / jsonpath

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

Incorrect return value when querying for array #20

Closed BorisKozo closed 4 years ago

BorisKozo commented 4 years ago

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:

{
  "firstName": "John",
  "lastName" : "doe",
  "age"      : 26,
  "address"  : {
    "streetAddress": "naist street",
    "city"         : "Nara",
    "postalCode"   : "630-0192"
  },
  "phoneNumbers": [
    {
      "type"  : "iPhone",
      "number": "0123-4567-8888"
    },
    {
      "type"  : "home",
      "number": "0123-4567-8910"
    }
  ]
}

Query: $.phoneNumbers[0:1] returns (correctly):

[
  {
    "type": "iPhone",
    "number": "0123-4567-8888"
  }
]

Query: $.phoneNumbers returns:

  [
    {
      "type": "iPhone",
      "number": "0123-4567-8888"
    },
    {
      "type": "home",
      "number": "0123-4567-8910"
    }
  ]

instead of (see the outer array):

[
  [
    {
      "type": "iPhone",
      "number": "0123-4567-8888"
    },
    {
      "type": "home",
      "number": "0123-4567-8910"
    }
  ]
]

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.

generikvault commented 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

whitlockjc commented 5 months ago

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.

whitlockjc commented 5 months ago

@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.

whitlockjc commented 5 months ago

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.

generikvault commented 5 months ago

@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.

whitlockjc commented 5 months ago

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.

whitlockjc commented 5 months ago

By the way, I appreciate you taking the time to explain/help.

generikvault commented 5 months ago

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]