← All Exercises
Resolve RFC 6901 JSON Pointers
intermediatejavascript
Background
JSON Pointer is the addressing syntax behind JSON Patch, $ref fragments, and configuration lookups. Correct escape handling matters because keys may legitimately contain slashes and tildes.
Problem
Implement resolvePointer(doc, pointer) following RFC 6901. The empty pointer addresses the whole document. Other pointers must start with a slash and are split into tokens, where ~1 means slash and ~0 means tilde. Array indexes must be canonical non-negative integers. Return an object with found and, when found, value.
Input
A JSON object with doc (any JSON value) and pointer (a string).
Output
A JSON object with found:true and the resolved value, or found:false when the path does not exist.
Sample tests
Sample 1
Input: {"doc":{"users":[{"name":"Ada","roles":["admin","dev"]}],"meta":{"a/b":1,"m~n":2}},"pointer":"/users/0/roles/1"}Expected: {"found":true,"value":"dev"}Sample 2
Input: {"doc":{"users":[{"name":"Ada","roles":["admin","dev"]}],"meta":{"a/b":1,"m~n":2}},"pointer":"/meta/a~1b"}Expected: {"found":true,"value":1}Sample 3
Input: {"doc":{"users":[{"name":"Ada","roles":["admin","dev"]}],"meta":{"a/b":1,"m~n":2}},"pointer":"/meta/m~0n"}Expected: {"found":true,"value":2}Sample 4
Input: {"doc":{"users":[{"name":"Ada"}]},"pointer":"/users/2/name"}Expected: {"found":false}Sample 5
Input: {"doc":{"a":1},"pointer":""}Expected: {"found":true,"value":{"a":1}}Your solution
Learning objective
Understand RFC 6901 pointer syntax, token escaping order, and safe traversal of JSON documents.