JSON Pointer (RFC 6901)
← run 2 · raw JSON · challenge definitions
82
score
tests (60%)7/10 passed
deliverables (20%)100%
content checks (20%)100%
01 what the model was asked
Create jsonpointer.py with exactly one function:
resolve(doc, pointer: str)
Resolve an RFC 6901 JSON Pointer against doc (nested dicts/lists) and return the referenced value.
Rules:
- The empty pointer "" refers to the whole document.
- A pointer is a sequence of /-prefixed reference tokens: "/a/b" means doc["a"]["b"].
- In a token, the escape ~1 decodes to "/" and ~0 decodes to "~" (decode ~1 before ~0).
- Tokens indexing a list are decimal indices: "/items/0" is the first element.
- Raise KeyError for anything that does not resolve (missing key, bad or out-of-range index, pointer not starting with "/", descending into a scalar).
Use only the Python standard library.
required deliverables + checks
✓deliverable: jsonpointer.py
✓jsonpointer.py must contain “def resolve”
pytest suite it was graded against
test_jsonpointer.py · 56 lines · 996 B
import pytest from jsonpointer import resolve DOC = { "foo": ["bar", "baz"], "": 0, "a/b": 1, "m~n": 8, "nested": {"list": [{"x": 42}]},} def test_whole_document(): assert resolve(DOC, "") is DOC def test_object_key(): assert resolve(DOC, "/foo") == ["bar", "baz"] def test_array_index(): assert resolve(DOC, "/foo/0") == "bar" def test_empty_key(): assert resolve(DOC, "/") == 0 def test_escaped_slash(): assert resolve(DOC, "/a~1b") == 1 def test_escaped_tilde(): assert resolve(DOC, "/m~0n") == 8 def test_deep_path(): assert resolve(DOC, "/nested/list/0/x") == 42 def test_missing_key_raises(): with pytest.raises(KeyError): resolve(DOC, "/nope") def test_bad_index_raises(): with pytest.raises(KeyError): resolve(DOC, "/foo/2") with pytest.raises(KeyError): resolve(DOC, "/foo/x") def test_pointer_without_slash_raises(): with pytest.raises(KeyError): resolve(DOC, "foo")
02 what it did — full transcript
promptthe exact instruction sent to the model
view full prompt
You are completing a coding task in one shot. Write complete files that satisfy the spec below. Tests (pytest) will be run against your files; you cannot run them yourself.
Reply with ONE JSON object and nothing else:
{"files": {"<relative path>": "<complete file content>", ...}}
Required deliverable files: jsonpointer.py
SPEC:
Create jsonpointer.py with exactly one function:
resolve(doc, pointer: str)
Resolve an RFC 6901 JSON Pointer against doc (nested dicts/lists) and return the referenced value.
Rules:
- The empty pointer "" refers to the whole document.
- A pointer is a sequence of /-prefixed reference tokens: "/a/b" means doc["a"]["b"].
- In a token, the escape ~1 decodes to "/" and ~0 decodes to "~" (decode ~1 before ~0).
- Tokens indexing a list are decimal indices: "/items/0" is the first element.
- Raise KeyError for anything that does not resolve (missing key, bad or out-of-range index, pointer not starting with "/", descending into a scalar).
Use only the Python standard library.
reply225 tokens
view raw reply
```json
{
"files": {
"jsonpointer.py": "def resolve(doc, pointer: str):\n if pointer == \"\":\n return doc\n tokens = pointer[1:].split('/') if pointer != '' else []\n for token in tokens:\n if token.startswith('~'): # escape sequences\n if token == '~1':\n token = '/'\n elif token == '~0':\n token = '~'\n else:\n raise ValueError(f\"Invalid escape sequence: {token}\")\n if isinstance(doc, dict):\n doc = doc[token]\n elif isinstance(doc, list) and token.isdigit(): # handle list indexing\n index = int(token)\n if index < 0 or index >= len(doc):\n raise IndexError(f\"Index out of range: {index}\")\n doc = doc[index]\n else:\n raise KeyError(f\"Key not found: {token}\")\n return doc"
}
}
```write_filejsonpointer.py · 816 B
graded 82.07 passed, 3 failed
pytest output
....FF..F. [100%]
=================================== FAILURES ===================================
______________________________ test_escaped_slash ______________________________
test_jsonpointer.py:31: in test_escaped_slash
assert resolve(DOC, "/a~1b") == 1
^^^^^^^^^^^^^^^^^^^^^
jsonpointer.py:14: in resolve
doc = doc[token]
^^^^^^^^^^
E KeyError: 'a~1b'
______________________________ test_escaped_tilde ______________________________
test_jsonpointer.py:35: in test_escaped_tilde
assert resolve(DOC, "/m~0n") == 8
^^^^^^^^^^^^^^^^^^^^^
jsonpointer.py:14: in resolve
doc = doc[token]
^^^^^^^^^^
E KeyError: 'm~0n'
____________________________ test_bad_index_raises _____________________________
test_jsonpointer.py:49: in test_bad_index_raises
resolve(DOC, "/foo/2")
jsonpointer.py:18: in resolve
raise IndexError(f"Index out of range: {index}")
E IndexError: Index out of range: 2
=========================== short test summary info ============================
FAILED test_jsonpointer.py::test_escaped_slash - KeyError: 'a~1b'
FAILED test_jsonpointer.py::test_escaped_tilde - KeyError: 'm~0n'
FAILED test_jsonpointer.py::test_bad_index_raises - IndexError: Index out of ...
3 failed, 7 passed in 0.04s
03 what it wrote
1
files
22
lines
22
source lines
1
functions
0
classes
12
cyclomatic
5
max nesting
22.0
avg fn lines
stdlib imports: none
| File | LOC | SLOC | Fns | Complexity | Depth | Imports |
|---|---|---|---|---|---|---|
| jsonpointer.py | 22 | 22 | 1 | 12 | 5 | — |
jsonpointer.py · 22 lines · 816 B
def resolve(doc, pointer: str): if pointer == "": return doc tokens = pointer[1:].split('/') if pointer != '' else [] for token in tokens: if token.startswith('~'): # escape sequences if token == '~1': token = '/' elif token == '~0': token = '~' else: raise ValueError(f"Invalid escape sequence: {token}") if isinstance(doc, dict): doc = doc[token] elif isinstance(doc, list) and token.isdigit(): # handle list indexing index = int(token) if index < 0 or index >= len(doc): raise IndexError(f"Index out of range: {index}") doc = doc[index] else: raise KeyError(f"Key not found: {token}") return doc
04 how it was graded
1
model calls
0
invalid actions
0
self test runs
225
tokens out
—
tokens in
22.6s
wall time
agent actions: —
final pytest output
....FF..F. [100%]
=================================== FAILURES ===================================
______________________________ test_escaped_slash ______________________________
test_jsonpointer.py:31: in test_escaped_slash
assert resolve(DOC, "/a~1b") == 1
^^^^^^^^^^^^^^^^^^^^^
jsonpointer.py:14: in resolve
doc = doc[token]
^^^^^^^^^^
E KeyError: 'a~1b'
______________________________ test_escaped_tilde ______________________________
test_jsonpointer.py:35: in test_escaped_tilde
assert resolve(DOC, "/m~0n") == 8
^^^^^^^^^^^^^^^^^^^^^
jsonpointer.py:14: in resolve
doc = doc[token]
^^^^^^^^^^
E KeyError: 'm~0n'
____________________________ test_bad_index_raises _____________________________
test_jsonpointer.py:49: in test_bad_index_raises
resolve(DOC, "/foo/2")
jsonpointer.py:18: in resolve
raise IndexError(f"Index out of range: {index}")
E IndexError: Index out of range: 2
=========================== short test summary info ============================
FAILED test_jsonpointer.py::test_escaped_slash - KeyError: 'a~1b'
FAILED test_jsonpointer.py::test_escaped_tilde - KeyError: 'm~0n'
FAILED test_jsonpointer.py::test_bad_index_raises - IndexError: Index out of ...
3 failed, 7 passed in 0.04s