Skip to content

Commit 1b83307

Browse files
committed
Allow raw numeric values to be used as keys
Fixes #123
1 parent 58378a6 commit 1b83307

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

jsonpath_ng/parser.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,12 @@ def p_jsonpath_parens(self, p):
149149
# Because fields in brackets cannot be '*' - that is reserved for array indices
150150
def p_fields_or_any(self, p):
151151
"""fields_or_any : fields
152-
| '*' """
152+
| '*'
153+
| NUMBER"""
153154
if p[1] == '*':
154155
p[0] = ['*']
156+
elif isinstance(p[1], int):
157+
p[0] = str(p[1])
155158
else:
156159
p[0] = p[1]
157160

tests/test_jsonpath.py

+5
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,11 @@ def test_update(parse, expression, data, update_value, expected_value):
276276
# --------
277277
#
278278
("A.'a.c'", {"A": {"a.c": "d"}}, ["d"], ["A.'a.c'"]),
279+
#
280+
# Numeric keys
281+
# --------
282+
#
283+
("1", {"1": "foo"}, ["foo"], ["1"]),
279284
)
280285

281286

tests/test_parser.py

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#
1313
("foo", Fields("foo")),
1414
("*", Fields("*")),
15+
("1", Fields("1")),
1516
("baz,bizzle", Fields("baz", "bizzle")),
1617
("[1]", Index(1)),
1718
("[1:]", Slice(start=1)),

0 commit comments

Comments
 (0)