8000 Add support for `parent` named operator · jeremydw/python-jsonpath-rw@27cdaad · GitHub
[go: up one dir, main page]

Skip to content

Commit 27cdaad

Browse files
committed
Add support for parent named operator
1 parent f4281ba commit 27cdaad

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

jsonpath_rw/jsonpath.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,27 @@ def __str__(self):
236236
def __repr__(self):
237237
return '%s(%r, %r)' % (self.__class__.__name__, self.left, self.right)
238238

239+
class Parent(JSONPath):
240+
"""
241+
JSONPath that matches the parent node of the current match.
242+
Will crash if no such parent exists.
243+
Available via named operator `parent`.
244+
"""
245+
246+
def find(self, datum):
247+
datum = DatumInContext.wrap(datum)
248+
return [datum.context]
249+
250+
def __eq__(self, other):
251+
return isinstance(other, Parent)
252+
253+
def __str__(self):
254+
return '`parent`'
255+
256+
def __repr__(self):
257+
return 'Parent()'
258+
259+
239260
class Where(JSONPath):
240261
"""
241262
JSONPath that first matches the left, and then

0 commit comments

Comments
 (0)
0