h2non / jsonpath-ng

Finally, a JSONPath implementation for Python that aims to be standard compliant. That's all. Enjoy!
Apache License 2.0
582 stars 85 forks source link

way to combine array operations? #50

Open taybin opened 4 years ago

taybin commented 4 years ago

Is there a way to both filter an array and get an index or slice of the filtered values?

Something like $.foo[?(@value>=3)],[1:5].baz, which would filter for filter for objects with value lte 3 and then return items 1-5 of the filtered objects. Or maybe $.foo[?(@value>=3),1:5].baz? Not sure what appropriate syntax would be for this.

I've looked at a bunch of jsonpath and similar (objectpath, sakstig) implementations and none seem to have a technique for this use case.

thibaut-pro commented 4 years ago

Looking for this as well.

This syntax works on https://jsonpath.com/: $.phoneNumbers[1, ?(@.type=="home")] With sample data:

{
  "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"
    },
    {
      "type"  : "home",
      "number": "0123-4567-8911"
    }
  ]
}

will return

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