cyberwombat
cyberwombat

Reputation: 40064

JSONPath or JMESPath filtering against another expression

I am struggling to find a way to filter nodes against another expression rather than a string or number. I have looked at the specs for both JSONPath and JMESPath and find a little info on the inability to do this the way I have been trying.

JSONPath

"With JSONPath square brackets operate on the object or array addressed by the previous path fragment. Indices always start by 0"

I have found JSONPath plus which has a @parent property but it only appear to go up to immediate parent and I need to access from root (or work up the ladder)

JMESPath

The specs seem to indicate a filter should be expression compare expression but I cannot get it to work. There is a semi related issue and a note on current_node though it's a little unclear to me.

End of the day I am trying to achieve this:

Data

{ 
  level: 20,
  items: [{
    qty: 20,
    id: '123'
  }, {
    qty: 30,
    id: '234'
 }]
}

Expression:

$.items[?(@.qty==$.level)]

Expectations

To fetch all the items with a qty that matches level.

Suggestions on to achieve this? I am looking at expanding the JSONPath implementation for myself as it seems the only way. I am not as versed in JMESPath which I just discovered so perhaps there is actually a way to do this.

Upvotes: 4

Views: 2694

Answers (1)

cyberwombat
cyberwombat

Reputation: 40064

Well the fantastic author of JSONPath plus added the @root feature right away.

So now:

$.items[?(@[email protected])] // Could use @parent.level for shallow stuff too

Upvotes: 2

Related Questions