Skip to content

Conversation

@jg-rp
Copy link
Owner

@jg-rp jg-rp commented Sep 11, 2025

We currently support JSON pointers with negative array indices, but the JSON Pointer specification (RFC 6901) does not allow negative array indexes. See #115.

This PR changes JSONPointer.min_int_index to be 0, causing a JSONPointerIndexError when resolving a negative index against an array-like value.

JSONPointer.min_int_index can be changed to a suitably negative integer if you need to support negative indices in your JSON pointers.

New default behavior

from jsonpath import JSONPointer

data = {"some": {"thing": [1, 2, 3]}}
pointer = JSONPointer("/some/thing/-2")
pointer.resolve(data)  # jsonpath.exceptions.JSONPointerIndexError: pointer index error: array indices must be between 0 and 9007199254740991

Change the minimum allowed index

from jsonpath import JSONPointer

JSONPointer.min_int_index = -(2**53) + 1

data = {"some": {"thing": [1, 2, 3]}}
pointer = JSONPointer("/some/thing/-2")
print(pointer.resolve(data))  # 2

We've also refactored some JSONPointer internals so that an index-like pointer token can be resolved against an object value.

data = {"foo": {"-1": "bar"}}
pointer = JSONPointer("/foo/-1")
assert pointer.resolve(data) == "bar"

Because of this, min and max int indices are now checked at pointer resolution time because we need data to know if we're resolving against an array value or an object value.

@jg-rp
Copy link
Owner Author

jg-rp commented Sep 11, 2025

Another option is to change Pointer.min_int_index to be 0 by default. That way users can choose to support negative indices if they wish.

@jg-rp jg-rp changed the title Reject negative JSON Pointer array indices Fix JSON Pointer array indices and index-like object keys Sep 11, 2025
@jg-rp jg-rp merged commit 20b2bbd into main Sep 11, 2025
26 checks passed
@jg-rp jg-rp deleted the fix-115 branch September 11, 2025 13:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants