Skip to content

Commit 47bba25

Browse files
committed
fix: resolve issue #17 false positive errors and parsing bugs
1 parent 18eab0b commit 47bba25

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

jsonpath/jsonpath.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class JSONPath:
6666

6767
def __init__(self, expr: str):
6868
expr = self._parse_expr(expr)
69-
self.segments = expr.split(JSONPath.SEP)
69+
self.segments = [s for s in expr.split(JSONPath.SEP) if s]
7070
self.lpath = len(self.segments)
7171
logger.debug(f"segments : {self.segments}")
7272

@@ -95,6 +95,7 @@ def _parse_expr(self, expr):
9595
expr = JSONPath.REP_GET_QUOTE.sub(self._get_quote, expr)
9696
expr = JSONPath.REP_GET_BACKQUOTE.sub(self._get_backquote, expr)
9797
expr = JSONPath.REP_GET_BRACKET.sub(self._get_bracket, expr)
98+
expr = re.sub(r"\.(\.#B)", r"\1", expr)
9899
expr = JSONPath.REP_GET_PAREN.sub(self._get_paren, expr)
99100
# split
100101
expr = JSONPath.REP_DOUBLEDOT.sub(f"{JSONPath.SEP}..{JSONPath.SEP}", expr)
@@ -195,8 +196,8 @@ def _filter(self, obj, i: int, path: str, step: str):
195196
r = False
196197
try:
197198
r = self.eval_func(step, None, {"__obj": obj})
198-
except Exception as err:
199-
logger.error(err)
199+
except Exception:
200+
pass
200201
if r:
201202
self._trace(obj, i, path)
202203

tests/test_issues.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from jsonpath import JSONPath
2+
3+
4+
def test_issue_17():
5+
data = [
6+
{"time": "2023-01-02T20:32:01Z", "user": "user1"},
7+
{"time": "2023-01-02T20:32:03Z", "user": "user2"},
8+
{"time": "2023-01-02T20:32:03Z", "user": "user1"},
9+
]
10+
user = "user1"
11+
expr = f'$.[?(@.user=="{user}")]'
12+
result = JSONPath(expr).parse(data)
13+
assert len(result) == 2
14+
assert result[0]["user"] == "user1"
15+
assert result[1]["user"] == "user1"
16+
17+
18+
def test_issue_17_bracket_dot_normalization():
19+
data = {"store": "book"}
20+
# Case 1: Standard bracket notation
21+
assert JSONPath("$['store']").parse(data) == ["book"]
22+
# Case 2: Dot followed by bracket (should be normalized to bracket)
23+
assert JSONPath("$.['store']").parse(data) == ["book"]
24+
# Case 3: Recursive descent with bracket (should NOT be normalized)
25+
data_recursive = {"a": {"store": "book"}, "b": {"store": "paper"}}
26+
# $..['store'] should find all 'store' keys
27+
assert sorted(JSONPath("$..['store']").parse(data_recursive)) == ["book", "paper"]

0 commit comments

Comments
 (0)