JavaScript Object Notation (JSON) Pointer
A short string syntax for pointing at one specific value inside a JSON document.
Bryan, P., Ed., Zyp, K., and M. Nottingham, Ed., "JavaScript Object Notation (JSON) Pointer", RFC 6901, DOI 10.17487/RFC6901, April 2013, <https://www.rfc-editor.org/info/rfc6901>.
@misc{rfc6901,
series = {Request for Comments},
number = 6901,
howpublished = {RFC 6901},
publisher = {RFC Editor},
doi = {10.17487/RFC6901},
url = {https://www.rfc-editor.org/info/rfc6901},
author = {P. Bryan and K. Zyp and M. Nottingham},
title = {{JavaScript Object Notation (JSON) Pointer}},
pagetotal = 8,
year = 2013,
month = apr,
} RFC text
Contents
Internet Engineering Task Force (IETF) P. Bryan, Ed.
Request for Comments: 6901 Salesforce.com
Category: Standards Track K. Zyp
ISSN: 2070-1721 SitePen (USA)
M. Nottingham, Ed.
Akamai
April 2013
JavaScript Object Notation (JSON) Pointer
Abstract
JSON Pointer defines a string syntax for identifying a specific value
within a JavaScript Object Notation (JSON) document.
Status of This Memo
This is an Internet Standards Track document.
This document is a product of the Internet Engineering Task Force
(IETF). It represents the consensus of the IETF community. It has
received public review and has been approved for publication by the
Internet Engineering Steering Group (IESG). Further information on
Internet Standards is available in Section 2 of RFC 5741.
Information about the current status of this document, any errata,
and how to provide feedback on it may be obtained at
http://www.rfc-editor.org/info/rfc6901.
Copyright Notice
Copyright (c) 2013 IETF Trust and the persons identified as the
document authors. All rights reserved.
This document is subject to BCP 78 and the IETF Trust's Legal
Provisions Relating to IETF Documents
(http://trustee.ietf.org/license-info) in effect on the date of
publication of this document. Please review these documents
carefully, as they describe your rights and restrictions with respect
to this document. Code Components extracted from this document must
include Simplified BSD License text as described in Section 4.e of
the Trust Legal Provisions and are provided without warranty as
described in the Simplified BSD License.
Table of Contents
1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . 2
2. Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3. Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
4. Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . 3
5. JSON String Representation . . . . . . . . . . . . . . . . . . 4
6. URI Fragment Identifier Representation . . . . . . . . . . . . 5
7. Error Handling . . . . . . . . . . . . . . . . . . . . . . . . 6
8. Security Considerations . . . . . . . . . . . . . . . . . . . . 6
9. Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . 7
10. References . . . . . . . . . . . . . . . . . . . . . . . . . . 7
10.1. Normative References . . . . . . . . . . . . . . . . . . . 7
10.2. Informative References . . . . . . . . . . . . . . . . . . 7
1. Introduction
This specification defines JSON Pointer, a string syntax for
identifying a specific value within a JavaScript Object Notation
(JSON) document [RFC4627]. JSON Pointer is intended to be easily
expressed in JSON string values as well as Uniform Resource
Identifier (URI) [RFC3986] fragment identifiers.
2. Conventions
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
"SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this
document are to be interpreted as described in [RFC2119].
This specification expresses normative syntax rules using Augmented
Backus-Naur Form (ABNF) [RFC5234] notation.
3. Syntax
A JSON Pointer is a Unicode string (see [RFC4627], Section 3)
containing a sequence of zero or more reference tokens, each prefixed
by a '/' (%x2F) character.
Because the characters '~' (%x7E) and '/' (%x2F) have special
meanings in JSON Pointer, '~' needs to be encoded as '~0' and '/'
needs to be encoded as '~1' when these characters appear in a
reference token.
The ABNF syntax of a JSON Pointer is:
json-pointer = *( "/" reference-token )
reference-token = *( unescaped / escaped )
unescaped = %x00-2E / %x30-7D / %x7F-10FFFF
; %x2F ('/') and %x7E ('~') are excluded from 'unescaped'
escaped = "~" ( "0" / "1" )
; representing '~' and '/', respectively
It is an error condition if a JSON Pointer value does not conform to
this syntax (see Section 7).
Note that JSON Pointers are specified in characters, not as bytes.
4. Evaluation
Evaluation of a JSON Pointer begins with a reference to the root
value of a JSON document and completes with a reference to some value
within the document. Each reference token in the JSON Pointer is
evaluated sequentially.
Evaluation of each reference token begins by decoding any escaped
character sequence. This is performed by first transforming any
occurrence of the sequence '~1' to '/', and then transforming any
occurrence of the sequence '~0' to '~'. By performing the
substitutions in this order, an implementation avoids the error of
turning '~01' first into '~1' and then into '/', which would be
incorrect (the string '~01' correctly becomes '~1' after
transformation).
The reference token then modifies which value is referenced according
to the following scheme:
o If the currently referenced value is a JSON object, the new
referenced value is the object member with the name identified by
the reference token. The member name is equal to the token if it
has the same number of Unicode characters as the token and their
code points are byte-by-byte equal. No Unicode character
normalization is performed. If a referenced member name is not
unique in an object, the member that is referenced is undefined,
and evaluation fails (see below).
o If the currently referenced value is a JSON array, the reference
token MUST contain either:
* characters comprised of digits (see ABNF below; note that
leading zeros are not allowed) that represent an unsigned
base-10 integer value, making the new referenced value the
array element with the zero-based index identified by the
token, or
* exactly the single character "-", making the new referenced
value the (nonexistent) member after the last array element.
The ABNF syntax for array indices is:
array-index = %x30 / ( %x31-39 *(%x30-39) )
; "0", or digits without a leading "0"
Implementations will evaluate each reference token against the
document's contents and will raise an error condition if it fails to
resolve a concrete value for any of the JSON pointer's reference
tokens. For example, if an array is referenced with a non-numeric
token, an error condition will be raised. See Section 7 for details.
Note that the use of the "-" character to index an array will always
result in such an error condition because by definition it refers to
a nonexistent array element. Thus, applications of JSON Pointer need
to specify how that character is to be handled, if it is to be
useful.
Any error condition for which a specific action is not defined by the
JSON Pointer application results in termination of evaluation.
5. JSON String Representation
A JSON Pointer can be represented in a JSON string value. Per
[RFC4627], Section 2.5, all instances of quotation mark '"' (%x22),
reverse solidus '\' (%x5C), and control (%x00-1F) characters MUST be
escaped.
Note that before processing a JSON string as a JSON Pointer,
backslash escape sequences must be unescaped.
For example, given the JSON 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 following JSON strings evaluate to the accompanying values:
"" // the whole document
"/foo" ["bar", "baz"]
"/foo/0" "bar"
"/" 0
"/a~1b" 1
"/c%d" 2
"/e^f" 3
"/g|h" 4
"/i\\j" 5
"/k\"l" 6
"/ " 7
"/m~0n" 8
6. URI Fragment Identifier Representation
A JSON Pointer can be represented in a URI fragment identifier by
encoding it into octets using UTF-8 [RFC3629], while percent-encoding
those characters not allowed by the fragment rule in [RFC3986].
Note that a given media type needs to specify JSON Pointer as its
fragment identifier syntax explicitly (usually, in its registration
[RFC6838]). That is, just because a document is JSON does not imply
that JSON Pointer can be used as its fragment identifier syntax. In
particular, the fragment identifier syntax for application/json is
not JSON Pointer.
Given the same example document as above, the following URI fragment
identifiers evaluate to the accompanying values:
# // the whole document
#/foo ["bar", "baz"]
#/foo/0 "bar"
#/ 0
#/a~1b 1
#/c%25d 2
#/e%5Ef 3
#/g%7Ch 4
#/i%5Cj 5
#/k%22l 6
#/%20 7
#/m~0n 8
7. Error Handling
In the event of an error condition, evaluation of the JSON Pointer
fails to complete.
Error conditions include, but are not limited to:
o Invalid pointer syntax
o A pointer that references a nonexistent value
This specification does not define how errors are handled. An
application of JSON Pointer SHOULD specify the impact and handling of
each type of error.
For example, some applications might stop pointer processing upon an
error, while others may attempt to recover from missing values by
inserting default ones.
8. Security Considerations
A given JSON Pointer is not guaranteed to reference an actual JSON
value. Therefore, applications using JSON Pointer should anticipate
this situation by defining how a pointer that does not resolve ought
to be handled.
Note that JSON pointers can contain the NUL (Unicode U+0000)
character. Care is needed not to misinterpret this character in
programming languages that use NUL to mark the end of a string.
9. Acknowledgements
The following individuals contributed ideas, feedback, and wording to
this specification:
Mike Acar, Carsten Bormann, Tim Bray, Jacob Davies, Martin J.
Duerst, Bjoern Hoehrmann, James H. Manger, Drew Perttula, and
Julian Reschke.
10. References
10.1. Normative References
[RFC2119] Bradner, S., "Key words for use in RFCs to Indicate
Requirement Levels", BCP 14, RFC 2119, March 1997.
[RFC3629] Yergeau, F., "UTF-8, a transformation format of ISO
10646", STD 63, RFC 3629, November 2003.
[RFC3986] Berners-Lee, T., Fielding, R., and L. Masinter, "Uniform
Resource Identifier (URI): Generic Syntax", STD 66,
RFC 3986, January 2005.
[RFC4627] Crockford, D., "The application/json Media Type for
JavaScript Object Notation (JSON)", RFC 4627, July 2006.
[RFC5234] Crocker, D. and P. Overell, "Augmented BNF for Syntax
Specifications: ABNF", STD 68, RFC 5234, January 2008.
10.2. Informative References
[RFC6838] Freed, N., Klensin, J., and T. Hansen, "Media Type
Specifications and Registration Procedures", BCP 13,
RFC 6838, January 2013.
Authors' Addresses
Paul C. Bryan (editor)
Salesforce.com
Phone: +1 604 783 1481
EMail: pbryan@anode.ca
Kris Zyp
SitePen (USA)
Phone: +1 650 968 8787
EMail: kris@sitepen.com
Mark Nottingham (editor)
Akamai
EMail: mnot@mnot.net
ISO Plain Language
On this page
§1What JSON Pointer Is
This specification defines JSON Pointer, a string syntax for identifying a specific value within a JavaScript Object Notation (JSON) document. A JSON Pointer is meant to be easy to express both in a JSON string value and in a Uniform Resource Identifier (URI) fragment identifier, so that the same pointer can travel inside a document or inside a web address.
One string syntax, two places to put it: inside the JSON itself, or after the number sign in a web address.
§3Pointer Syntax
A JSON Pointer is a Unicode string containing a sequence of zero or more reference tokens, each prefixed by a '/' character. Because '~' and '/' have special meanings inside a pointer, a '~' in a token has to be written as '~0' and a '/' as '~1'.
The syntax in ABNF is:
json-pointer = *( "/" reference-token )
reference-token = *( unescaped / escaped )
unescaped = %x00-2E / %x30-7D / %x7F-10FFFF
; %x2F ('/') and %x7E ('~') are excluded from 'unescaped'
escaped = "~" ( "0" / "1" )
; representing '~' and '/', respectivelyA pointer that does not conform to this syntax is an error condition. The RFC also notes that JSON Pointers are specified in characters, not in bytes.
A pointer is a list of steps separated by slashes. Because the slash and the tilde are used by the syntax itself, they have to be escaped when they appear inside a name.
json-pointer = *( "/" reference-token )
reference-token = *( unescaped / escaped )
unescaped = %x00-2E / %x30-7D / %x7F-10FFFF
escaped = "~" ( "0" / "1" )§4Evaluating a Pointer
Evaluation starts at the root value of the document and ends at some value inside it. Each reference token is evaluated in turn.
Evaluation of a token begins by decoding the escaped character sequences. The order matters: '~1' is transformed to '/' first, and '~0' is transformed to '~' second. Doing it in that order avoids turning '~01' into '~1' and then into '/', which would be wrong, because '~01' should end up as '~1'.
If the value currently referenced is a JSON object, the new value is the member whose name the token names. The member name equals the token when it has the same number of Unicode characters and their code points match byte for byte, and no Unicode normalization is performed. If a member name is not unique in the object, the referenced member is undefined and evaluation fails.
If the value currently referenced is a JSON array, the token must contain either digits that represent an unsigned base-10 index, with no leading zero, so that the new value is the element at that zero-based index, or exactly the single character '-', which refers to the nonexistent member after the last element. The index syntax is array-index = %x30 / ( %x31-39 *(%x30-39) ).
An implementation evaluates each token against the document and raises an error if it cannot resolve a concrete value. Indexing an array with a non-numeric token is such an error, and so is using '-' to index an array, because it refers to an element that does not exist. Any error for which the application defines no specific action ends the evaluation.
Start at the top of the document and walk down one step at a time. A step names a field of an object or a numbered item of an array, and if a step does not make sense the whole pointer fails.
- Unescape in orderTurn ~1 into a slash first, then ~0 into a tilde, so that ~01 becomes ~1.
- Object memberThe token names the member, compared character by character with no normalization.
- Array indexDigits without a leading zero, counting from zero.
- The dashA single dash names the position after the last element, which never exists.
- Empty pointerThe empty string is the whole document.
§5Pointers Inside JSON Strings
A JSON Pointer can be written as a JSON string value. All the quotation mark, reverse solidus and control characters must be escaped, as every JSON string requires. Before a JSON string is processed as a pointer, its backslash escape sequences must be unescaped.
The RFC's example document has a member with every awkward name: "foo": ["bar", "baz"], a member with an empty name, and members named "a/b", "c%d", "e^f", "g|h", "i\\j", "k\"l", " " and "m~n". The pointers that select them are "" for the whole document, "/foo" for the array, "/foo/0" for its first element, "/" for the member with the empty name, "/a~1b" for the member named a/b, and "/m~0n" for the member named m~n.
In a JSON string, a pointer with a slash in a name uses the tilde escape, and the JSON escaping of backslashes and quotes still applies on top of that.
- the JSON document Every member name in this document needs care
- "foo" ["bar", "baz"]
- "" (empty name) 0
- "a/b" 1, reached as /a~1b
- "c%d" 2
- "m~n" 8, reached as /m~0n
| Target | In a JSON string | In a URI fragment |
|---|---|---|
| The whole document | "" | # |
| The array | "/foo" | #/foo |
| The first element of the array | "/foo/0" | #/foo/0 |
| The member with the empty name | "/" | #/ |
| The member named a/b | "/a~1b" | #/a~1b |
| The member named c%d | "/c%d" | #/c%25d |
| The member named m~n | "/m~0n" | #/m~0n |
§6Pointers in URI Fragments
A JSON Pointer can also be written as a URI fragment identifier by encoding it into octets with UTF-8 and then percent-encoding the characters that the fragment rule of RFC 3986 does not allow. So the same document that "/c%d" selects in a JSON string is #/c%25d in a URI, and the space in a member name is #/%20.
The RFC is careful about one point: a media type has to name JSON Pointer as its fragment identifier syntax explicitly, usually in its registration. Just because a document is JSON does not mean that JSON Pointer can be used as its fragment identifier syntax, and in particular the fragment identifier syntax for application/json is not JSON Pointer.
In a web address the pointer is percent-encoded on top of the usual escaping. A media type has to opt in to this syntax before a pointer can be used after the number sign.
§7Error Handling
When an error condition occurs, evaluation of the pointer fails to complete. Error conditions include invalid pointer syntax and a pointer that references a value that does not exist.
This specification does not define how errors are handled. An application of JSON Pointer should specify the impact and the handling of each kind of error. Some applications might stop processing at the first error, while others might recover from a missing value by inserting a default one.
The RFC says what counts as a failure but leaves the reaction to whoever uses pointers, because a database update and a web link want different things.
§8Security Considerations
A JSON Pointer is not guaranteed to reference a value that actually exists, so an application using pointers should define in advance how a pointer that does not resolve is handled.
The RFC also points out that a JSON pointer can contain the NUL character, so care is needed not to misinterpret that character in programming languages that use NUL to mark the end of a string.
A pointer may point at nothing, so the application has to say what to do about it. And a pointer can contain a null character, which can cut a string short in some languages.