JSONPath-Plus / JSONPath

A fork of JSONPath from http://goessner.net/articles/JsonPath/
Other
975 stars 175 forks source link

Filtering against another path component #106

Closed cyberwombat closed 5 years ago

cyberwombat commented 5 years ago

Is there a way with this lib to access another path value to use in filters? I see @parent but that only lets me go up one level. I have tried making a filter using root but that fails.

For example how would I fetch all the phoneNumbers that have the type that matches kind?

const path= "$.info.phoneNumbers[?(@.type==ROOT.kind)]"
const result = JSONPath({path, json:{
  "firstName": "John",
  "kind" : "home",
  "age"      : 26,
  "address"  : {
    "streetAddress": "naist street",
    "city"         : "Nara",
    "postalCode"   : "630-0192"
  },
  "info":  {
    "phoneNumbers": [
    {
      "type"  : "iPhone",
      "number": "0123-4567-8888"
    },
    {
      "type"  : "home",
      "number": "0123-4567-8910"
    }
  ]
}
}});
cyberwombat commented 5 years ago

lol - looks like you are adding @root right now. Thought I was going crazy when I saw that in the docs suddenly.

brettz9 commented 5 years ago

Yes :) Note though that you can't use filters or such on @root itself such that one could do full-blown intersections and such. But for your stated use case it should work. Note that you can also add your own variables to the sandbox, but I thought this one was useful enough.

@s3u : Hi... Would you mind setting the master branch for JSONPath on Github as the Github Pages branch, so the demo I've just added to master can be available? The steps are available at https://help.github.com/en/articles/configuring-a-publishing-source-for-your-github-pages-site .

Anyone else: I'm still too busy to dedicate much time to jsonpath-plus, but this one was easy enough to add.