| | |

JSON 11 ๐Ÿ“„ JSON Pointer (RFC 6901)

โฑ๏ธ Estimated read: 12 min | |

JSON Pointer is a string syntax for identifying a specific value within a JSON document. It is defined by RFC 6901, published in April 2013, and it is the addressing mechanism that the JSON Patch (RFC 6902) and the JSON Schema $ref keywords rely on. The pointer is not a query languageโ€”it does not search, filter, or return multiple values. It resolves to exactly one location, and the evaluation either succeeds or fails. The RFC states this plainly: “A JSON Pointer is a string syntax for identifying a specific value within a JavaScript Object Notation (JSON) document” . The syntax is intended to be easily expressed in JSON string values as well as in URI fragment identifiers.

The pointer is a sequence of zero or more reference tokens, each prefixed by a / character. The empty string is a valid pointer that references the whole document. The /foo references the foo member of the root object, and the /foo/bar references the bar member of the foo object . The syntax is deliberately simple, and the simplicity is the mechanism that makes the pointer portable across the languages and the implementations.

Key point: A JSON Pointer is a /-separated sequence of reference tokens. The empty string references the whole document. The ~0 escape represents ~, and the ~1 escape represents /. The - token is the special array index that means “the position after the last element,” and it is used by JSON Patch for the append operations. The evaluation is sequential, and a missing token fails the evaluation.


Why JSON Pointer exists

The addressing problem. A JSON document is a tree, and the tools that manipulate the documentโ€”the patch libraries, the schema validators, the configuration editorsโ€”need a way to refer to the specific node in the tree. The JSON Pointer is the addressing mechanism, and the address is the string that the tool stores, transmits, and resolves . The JSON Path is the query language that returns the multiple nodes; the JSON Pointer is the address that returns the single node. The two are the different tools for the different problems.

The escaping problem. The / is the token separator, and the ~ is the escape character. A member name that contains the / or the ~ must be escaped, or the pointer would be parsed as the different path. The RFC defines the ~1 for the / and the ~0 for the ~, and the order of the unescaping matters: the ~1 must be transformed first, then the ~0, because the ~01 would otherwise become the ~1 and then the / . The escaping is the mechanism that makes the arbitrary member names addressable.

The array problem. The array elements are addressed by the zero-based index, and the index must be a digit sequence without the leading zeros . The - token is the exception: it refers to the nonexistent element after the last array element, and it is the mechanism that JSON Patch uses for the append operation . The - cannot be used for the retrieval, because the element does not exist; it is the mutation-only token.

The URI problem. The JSON Pointer can be used as the URI fragment identifier, and the characters that are not allowed in the fragment must be percent-encoded . The /foo/inner%20object/baz is the pointer that resolves to the baz member of the inner object member of the foo member . The percent-encoding is the mechanism that makes the pointer the valid URI fragment.

The failure problem. The pointer evaluation can fail: the token does not match the member, the array index is out of bounds, or the value is not the object or the array. The RFC states that the evaluation fails in these cases, and the failure is the mechanism that the patch libraries use to reject the invalid patch . The pointer is not guaranteed to resolve to a value, and the implementations must be aware of this .


a. The syntax and the escaping rules

A JSON Pointer is a Unicode string containing a sequence of zero or more reference tokens, each prefixed by a / character. The ABNF syntax is json-pointer = *( "/" reference-token ), and the reference token is the sequence of the unescaped characters or the escaped pairs .

// The JSON Pointer syntax
json-pointer    = *( "/" reference-token )
reference-token = *( unescaped / escaped )
unescaped       = %x00-2E / %x30-7D / %x7F-10FFFF
                  ; %x2F ('/') and %x7E ('~') are excluded
escaped         = "~" ( "0" / "1" )

The %x2F is the /, and the %x7E is the ~. The two characters are excluded from the unescaped set, and the escaped forms are the only way to represent them in the token . The pointer is specified in the characters, not the bytes, and the Unicode is the encoding.

The escaping rules are the two substitutions. The ~ becomes the ~0, and the / becomes the ~1. The unescaping is the reverse, and the order is the critical part: the ~1 must be transformed to the / first, and the ~0 must be transformed to the ~ second . The reverse order would turn the ~01 into the ~1 and then into the /, which is the wrong result.

// The escape and the unescape
function escapeToken(token) {
  // The ~ must be escaped first.
  return token.replace(/~/g, "~0").replace(/\//g, "~1");
}

function unescapeToken(token) {
  // The ~1 must be unescaped first.
  return token.replace(/~1/g, "/").replace(/~0/g, "~");
}

// The example
escapeToken("a/b~c");   // "a~1b~0c"
unescapeToken("a~1b~0c"); // "a/b~c"

The Go implementation shows the same pattern: the Escape function replaces the ~ with the ~0 and the / with the ~1, and the Unescape function replaces the ~0 with the ~ and the ~1 with the / . The Dart implementation notes the order explicitly: the ~1 must be un-escaped before the ~0 when parsing, and the ~ must be escaped before the / when serializing .


b. The evaluation and the special tokens

The evaluation begins with the reference to the root value of the JSON document, and it completes with the reference to the specific value. Each reference token is evaluated sequentially, and the token modifies the referenced value .

If the currently referenced value is a JSON object, the new referenced value is the object member with the name identified by the token. The member name is equal to the token if it has the same number of the Unicode characters and their code points are byte-by-byte equal. No Unicode normalization is performed . If the member name is not unique, the member that is referenced is undefined, and the evaluation fails.

If the currently referenced value is a JSON array, the token must contain either the digits (without the leading zeros) that represent the unsigned base-10 integer, or the single character - . The digit token makes the new referenced value the array element with the zero-based index. The - token makes the new referenced value the nonexistent element after the last array element.

// The evaluation
const document = {
  foo: ["bar", "baz"],
  "": 0,
  "a/b": 1,
  "c%d": 2,
  "e^f": 3,
  "g|h": 4,
  "i\\j": 5,
  "k\"l": 6,
  " ": 7,
  "m~n": 8,
};

// The empty string references the whole document.
// The "/foo" references the array ["bar", "baz"].
// The "/foo/0" references the "bar".
// The "/" references the "" member, which is 0.
// The "/a~1b" references the "a/b" member, which is 1.
// The "/m~0n" references the "m~n" member, which is 8.

The - token is the special case. The RFC states that the - token refers to the nonexistent element after the last array element, and it is the mechanism that JSON Patch uses for the append operation . The token cannot be used for the retrieval, because the element does not exist; the evaluation of the - in the retrieval context fails. The JSON Pointer tutorial states this explicitly: the - is unique to JSON Pointer, and it is primarily useful for the JSON Patch operations .

The URI fragment representation is the second use. The pointer can be used as the URI fragment, and the characters that are not allowed in the fragment must be percent-encoded . The example from the draft shows the http://example.com/example.json#/foo/inner%20object/baz resolving to the baz member of the inner object member . The percent-encoding is the mechanism that makes the pointer the valid URI fragment.


Complete Example Session

// ============================================
// PART 1: THE ROOT POINTER
// ============================================

const document = {
  foo: ["bar", "baz"],
  "": 0,
  "a/b": 1,
  "m~n": 8,
};

// The empty string references the whole document.
const root = getPointer(document, "");
// The root is the whole document object.

// ============================================
// PART 2: THE OBJECT MEMBER
// ============================================

// The "/foo" references the foo member.
const foo = getPointer(document, "/foo");
// The foo is the array ["bar", "baz"].

// ============================================
// PART 3: THE ARRAY ELEMENT
// ============================================

// The "/foo/0" references the first element.
const first = getPointer(document, "/foo/0");
// The first is "bar".

// ============================================
// PART 4: THE EMPTY MEMBER NAME
// ============================================

// The "/" references the "" member.
const empty = getPointer(document, "/");
// The empty is 0.

// ============================================
// PART 5: THE ESCAPED SLASH
// ============================================

// The "/a~1b" references the "a/b" member.
const slash = getPointer(document, "/a~1b");
// The slash is 1.

// ============================================
// PART 6: THE ESCAPED TILDE
// ============================================

// The "/m~0n" references the "m~n" member.
const tilde = getPointer(document, "/m~0n");
// The tilde is 8.

// ============================================
// PART 7: THE APPEND TOKEN
// ============================================

// The "/foo/-" is the append position.
// It is used by the JSON Patch for the append.
// The retrieval fails because the element does not exist.

// ============================================
// PART 8: THE URI FRAGMENT
// ============================================

// The pointer as the URI fragment.
const uri = "http://example.com/example.json#/foo/0";
// The fragment is the "/foo/0".
// The browser decodes the fragment and the pointer is evaluated.

// ============================================
// PART 9: THE ESCAPE FUNCTIONS
// ============================================

// The escape
function escapeToken(token) {
  return token.replace(/~/g, "~0").replace(/\//g, "~1");
}

// The unescape
function unescapeToken(token) {
  return token.replace(/~1/g, "/").replace(/~0/g, "~");
}

// ============================================
// PART 10: THE CHECKLIST
// ============================================

// [ ] The pointer is the /-separated tokens.
// [ ] The empty string references the whole document.
// [ ] The ~0 is the ~ and the ~1 is the /.
// [ ] The ~1 is unescaped before the ~0.
// [ ] The array index is the digits without the leading zeros.
// [ ] The - token is the append position.
// [ ] The URI fragment is percent-encoded.

The ten parts covered the root pointer, the object member, the array element, the empty member name, the escaped slash, the escaped tilde, the append token, the URI fragment, the escape functions, and the checklist. The through-line is that the pointer is the address, and the escaping is the mechanism that makes the arbitrary names addressable.


Quick Reference

The syntax

ElementMeaning
""The whole document
/The member with the empty name
/fooThe foo member
/foo/0The first element of the foo array
/foo/-The append position after the last element

The escaping

EncodedDecoded
~0~
~1/

The evaluation rules

Current valueTokenResult
ObjectThe member nameThe member value
ArrayThe digitsThe element at the index
Array-The nonexistent element after the last
ArrayThe non-digitThe evaluation fails

The implementations

LanguagePackageNotes
GojsonpointerThe Escape and the Unescape functions
Dartrfc_6901The read and the write, the - support
JavaScriptjsonpointerThe common implementation
OCamljson-pointerThe type-safe interface

Best Practices

โœ… Do This:

// Escape the ~ before the /
escapeToken("a/b~c"); // "a~1b~0c"                              // โœ…

// Unescape the ~1 before the ~0
unescapeToken("a~1b~0c"); // "a/b~c"                            // โœ…

// Use the - for the append
// The /foo/- is the append position.                           // โœ…

โŒ Don’t Do This:

// Use the leading zeros in the array index
// The /foo/01 is invalid.                                      // โŒ

// Use the - for the retrieval
// The /foo/- does not resolve to a value.                      // โŒ

Common Pitfalls

PitfallWhy It HappensFix
The pointer does not resolveThe member name is not escapedUse the ~1 and the ~0
The array index is wrongThe leading zero is usedRemove the leading zero
The append failsThe - is used for the retrievalUse the - only for the patch
The URI fragment is malformedThe characters are not percent-encodedPercent-encode the fragment

Real-World Examples

1. The root pointer

getPointer(document, "");

2. The object member

getPointer(document, "/foo");

3. The array element

getPointer(document, "/foo/0");

4. The empty name

getPointer(document, "/");

5. The escaped slash

getPointer(document, "/a~1b");

6. The escaped tilde

getPointer(document, "/m~0n");

7. The append token

// The "/foo/-" is the append position.

8. The URI fragment

const uri = "http://example.com/example.json#/foo/0";

9. The escape

escapeToken("a/b"); // "a~1b"

10. The unescape

unescapeToken("a~1b"); // "a/b"

Visual

The Pointer Evaluation

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  THE POINTER EVALUATION                                      โ”‚
โ”‚                                                              โ”‚
โ”‚  The pointer: /foo/0                                         โ”‚
โ”‚         โ”‚                                                    โ”‚
โ”‚         โ–ผ                                                    โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                                    โ”‚
โ”‚  โ”‚ The root document   โ”‚  The starting value                โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                                    โ”‚
โ”‚             โ”‚                                                โ”‚
โ”‚             โ–ผ                                                    โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                                    โ”‚
โ”‚  โ”‚ The "foo" token     โ”‚  The object member                 โ”‚
โ”‚  โ”‚ โ†’ the foo array     โ”‚                                    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                                    โ”‚
โ”‚             โ”‚                                                โ”‚
โ”‚             โ–ผ                                                    โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                                    โ”‚
โ”‚  โ”‚ The "0" token       โ”‚  The array element                 โ”‚
โ”‚  โ”‚ โ†’ the "bar"         โ”‚                                    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                                    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The Escaping Order

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  THE ESCAPING ORDER                                          โ”‚
โ”‚                                                              โ”‚
โ”‚  The unescape:                                               โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                                    โ”‚
โ”‚  โ”‚ ~1 โ†’ /              โ”‚  First                            โ”‚
โ”‚  โ”‚ ~0 โ†’ ~              โ”‚  Second                           โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                                    โ”‚
โ”‚                                                              โ”‚
โ”‚  The wrong order:                                            โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                                    โ”‚
โ”‚  โ”‚ ~0 โ†’ ~              โ”‚  First                            โ”‚
โ”‚  โ”‚ ~1 โ†’ /              โ”‚  Second                           โ”‚
โ”‚  โ”‚ ~01 โ†’ ~1 โ†’ /        โ”‚  The double-escape bug            โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                                    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The URI Fragment

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  THE URI FRAGMENT                                            โ”‚
โ”‚                                                              โ”‚
โ”‚  http://example.com/example.json#/foo/inner%20object/baz     โ”‚
โ”‚                                                              โ”‚
โ”‚  The fragment:                                               โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                                    โ”‚
โ”‚  โ”‚ /foo/inner%20object โ”‚  The percent-encoded              โ”‚
โ”‚  โ”‚ /baz                โ”‚  The pointer                      โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                                    โ”‚
โ”‚             โ”‚                                                โ”‚
โ”‚             โ–ผ                                                    โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                                    โ”‚
โ”‚  โ”‚ The decoded pointer โ”‚  The "/foo/inner object/baz"      โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                                    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Summary

ItemValue
The syntaxThe /-separated reference tokens
The rootThe empty string
The escapeThe ~0 for the ~, the ~1 for the /
The unescape orderThe ~1 first, the ~0 second
The array indexThe digits without the leading zeros
The append tokenThe -
The URI fragmentThe percent-encoded pointer

Key takeaways:

  • The JSON Pointer is the address, not the query. It resolves to exactly one location, and the evaluation fails if the token does not match. The JSON Path is the query that returns the multiple nodes, and the two are the different tools for the different problems .
  • The ~0 and the ~1 are the escape sequences. The ~0 represents the ~, and the ~1 represents the /. The unescaping order matters: the ~1 must be transformed first, then the ~0, or the ~01 becomes the / .
  • The array index must be the digits without the leading zeros. The - token is the special append position, and it is used by the JSON Patch for the append operations .
  • The URI fragment is the percent-encoded pointer. The characters that are not allowed in the fragment must be percent-encoded, and the browser decodes the fragment before the evaluation .
  • The evaluation can fail. The missing member, the out-of-bounds index, and the non-object or non-array value are the failure cases. The pointer is not guaranteed to resolve, and the implementations must be aware of this .

Remember: The JSON Pointer is the string that addresses the specific value in the JSON document. It is the /-separated tokens, the ~0 and the ~1 escapes, the array index, and the - append token. The evaluation is sequential, and the failure is the mechanism that the patch libraries use to reject the invalid patch. The pointer is the address, and the JSON Path is the query.

Was this helpful?


Stop using slow, ad-bloated tool sites! ๐Ÿคฎ

๐Ÿ”Ž Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
โœ… Finance (Mortgage, Interest, Inflation)
โœ… Tech (Base64, JSON, Dev Suite, IP)
โœ… Health (BMI, BMR, TDEE)
โœ… Productivity (Timer, Workspace, QR)

โšก๏ธ Fast & Private
๐Ÿ”’ No data leaves your device
๐Ÿ’Ž 100% Free

๐Ÿ”— Use it now: https://tools.kandz.me
๐Ÿ”– Bookmark itโ€”youโ€™ll need it later!