Skip to content

Commit

Permalink
Allow raw numeric values to be used as keys
Browse files Browse the repository at this point in the history
Fixes #123
  • Loading branch information
michaelmior committed Oct 10, 2024
1 parent 58378a6 commit 1b83307
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
5 changes: 4 additions & 1 deletion jsonpath_ng/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,12 @@ def p_jsonpath_parens(self, p):
# Because fields in brackets cannot be '*' - that is reserved for array indices
def p_fields_or_any(self, p):
"""fields_or_any : fields
| '*' """
| '*'
| NUMBER"""
if p[1] == '*':
p[0] = ['*']
elif isinstance(p[1], int):
p[0] = str(p[1])
else:
p[0] = p[1]

Expand Down
5 changes: 5 additions & 0 deletions tests/test_jsonpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ def test_update(parse, expression, data, update_value, expected_value):
# --------
#
("A.'a.c'", {"A": {"a.c": "d"}}, ["d"], ["A.'a.c'"]),
#
# Numeric keys
# --------
#
("1", {"1": "foo"}, ["foo"], ["1"]),
)


Expand Down
1 change: 1 addition & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#
("foo", Fields("foo")),
("*", Fields("*")),
("1", Fields("1")),
("baz,bizzle", Fields("baz", "bizzle")),
("[1]", Index(1)),
("[1:]", Slice(start=1)),
Expand Down

0 comments on commit 1b83307

Please sign in to comment.