Open the context
Choose the project folder and name the files or behavior involved.
Resolve a JSON Pointer against dictionaries and lists without editing them. Test the empty root pointer and escaped keys, rejecting malformed escapes, missing members and noncanonical array indices.
Choose the project folder and name the files or behavior involved.
Describe the input, expected output and boundaries that must stay intact.
Review the proposed edits and any commands before relying on their result.
Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
import re
def pointer_get(root, pointer):
if not isinstance(pointer, str): raise ValueError("pointer text")
if pointer == "": return root
if not pointer.startswith("/"): raise ValueError("root slash")
value = root
for raw in pointer[1:].split("/"):
if re.search(r"~(?![01])", raw): raise ValueError("escape")
key = raw.replace("~1", "/").replace("~0", "~")
if isinstance(value, dict): value = value[key]
elif isinstance(value, list):
if not re.fullmatch(r"0|[1-9][0-9]*", key):
raise ValueError("array index")
if len(key) > len(str(len(value))): raise IndexError(key)
value = value[int(key)]
else: raise ValueError("not a container")
return valueThe example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.