<?xml version="1.0" encoding="US-ASCII"?>
<!DOCTYPE rfc [
<!ENTITY RFC2119 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.2119.xml">
<!ENTITY RFC6901 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.6901.xml">
<!ENTITY RFC8259 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.8259.xml">
]>
<?rfc toc="yes"?>
<?rfc symrefs="yes"?>
<?rfc compact="yes"?>
<?rfc subcompact="no"?>
<?rfc strict="no"?>
<?rfc rfcedstyle="yes"?>
<?rfc comments="yes"?>
<?rfc inline="yes" ?>
<rfc category="info" docName="draft-hha-relative-json-pointer-00" ipr="trust200902" submissionType="IETF">
    <front>
        <title abbrev="Relative JSON Pointers">Relative JSON Pointers</title>

        <author fullname="Geraint Luff" initials="G" surname="Luff">
            <address>
                <postal>
                    <street></street>
                    <city>Cambridge</city>
                    <country>UK</country>
                </postal>
                <email>luffgd@gmail.com</email>
            </address>
        </author>

        <author fullname="Henry H. Andrews" initials="H" surname="Andrews" role="editor">
            <address>
                <email>andrews_henry@yahoo.com</email>
            </address>
        </author>

        <author fullname="Ben Hutton" initials="B" surname="Hutton" role="editor">
            <address>
                <email>ben@jsonschema.dev</email>
                <uri>https://jsonschema.dev</uri>
            </address>
        </author>

        <date year="2023"/>
        <workgroup>Internet Engineering Task Force</workgroup>
        <keyword>JSON</keyword>
        <keyword>JavaScript</keyword>
        <keyword>Object</keyword>
        <keyword>Notation</keyword>


        <abstract>
            <t>
                JSON Pointer is a syntax for specifying locations in a JSON document,
                starting from the document root.  This document defines an extension
                to the JSON Pointer syntax, allowing relative locations from within
                the document.
            </t>
        </abstract>
    </front>

    <middle>
        <section title="Introduction">
            <t>
                JSON Pointer (<xref target="RFC6901">RFC 6901</xref>) is a syntax for specifying
                locations in a JSON document, starting from the document root.  This
                document defines a related syntax allowing identification of relative locations
                from within the document.
            </t>
        </section>

        <section title="Conventions and Terminology">
            <t>
                <!-- The text in this section has been copied from the official boilerplate,
                and should not be modified.-->

                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 <xref target="RFC2119">RFC 2119</xref>.
            </t>
        </section>

        <section title="Syntax">
            <t>
                A Relative JSON Pointer is a Unicode string in UTF-8 encoding (see RFC 8259,
                <xref target="RFC8259">Section 8</xref>), comprising a non-negative integer,
                an optional index adjustment consisting of '+' (%x2B) or '-' (%x2D) followed
                by a positive integer, followed by either a '#' (%x23) character or
                a JSON Pointer (<xref target="RFC6901">RFC 6901</xref>).
            </t>
            <t>
                The separation between the integer prefix (with optional adjustment)
                and the JSON Pointer will
                always be unambiguous, because a JSON Pointer must be either zero-
                length or start with a '/' (%x2F).  Similarly, a JSON Pointer will
                never be ambiguous with the '#'.
            </t>
            <t>
                The ABNF syntax of a Relative JSON Pointer is:
            </t>
            <sourcecode type="abnf9110"><![CDATA[
  relative-json-pointer = origin-specification ( "#" / json-pointer )
                        ; json-pointer from RFC 6901

  origin-specification  = non-negative-integer [ index-manipulation ]
  index-manipulation    = ( "+" / "-" ) positive-integer
  non-negative-integer  = "0" / positive-integer
  positive-integer      = %x31-39 *DIGIT
                        ; digits without a leading zero
]]></sourcecode>
        </section>

        <section title="Evaluation">
            <t>
                Evaluation of a Relative JSON Pointer begins with a reference to a
                value within a JSON document, and completes with either a value
                within that document, a string corresponding to an object member, or
                integer value representing an array index.
            </t>
            <t>
                Evaluation begins by processing the non-negative-integer prefix.
                This can be found by taking the longest continuous sequence of decimal
                digits available, starting from the beginning of the string, taking
                the decimal numerical value. If the value is zero, the following steps
                are skipped. If this value is more than zero, then the following steps
                are repeated that number of times:
                <list>
                    <t>
                        If the current referenced value is the root of the document, then
                        evaluation fails (see below).
                    </t>
                    <t>
                        If the referenced value is an item within an array, then the new
                        referenced value is that array.
                    </t>
                    <t>
                        If the referenced value is an object member within an object, then
                        the new referenced value is that object.
                    </t>
                </list>
            </t>
            <t>
                If the next character is a plus ("+") or minus ("-"), followed by another
                continuous sequence of decimal digits, the following steps
                are taken using the decimal numeric value of that plus or minus sign
                and decimal sequence:
                <list>
                    <t>
                        If the current referenced value is not an item of an array,
                        then evaluation fails (see below).
                    </t>
                    <t>
                        If the referenced value is an item of an array, then the
                        new referenced value is the item of the array indexed by
                        adding the decimal value (which may be negative), to the
                        index of the current referenced value.
                    </t>
                </list>
            </t>
            <t>
                If the remainder of the Relative JSON Pointer is a JSON Pointer, then
                evaluation proceeds as per
                <xref target="RFC6901">RFC 6901, Section 5</xref>
                with the modification that the initial reference
                being used is the reference currently being held (which may not be
                root of the document).
            </t>
            <t>
                Otherwise (when the remainder of the Relative JSON Pointer is the
                character '#'), the final result is determined as follows:
                <list>
                    <t>
                        If the current referenced value is the root of the document, then
                        evaluation fails (see below).
                    </t>
                    <t>
                        If the referenced value is an item within an array, then the final
                        evaluation result is the value's index position within the array.
                    </t>
                    <t>
                        If the referenced value is an object member within an object, then
                        the new referenced value is the corresponding member name.
                    </t>
                </list>
            </t>
        </section>

        <section title="JSON String Representation">
            <t>
                The concerns surrounding JSON String representation of a Relative
                JSON Pointer are identical to those laid out in
                <xref target="RFC6901">RFC 6901, Section 5</xref>.
            </t>
            <section title="Examples">
                <figure align="center">
                    <preamble>
                        For example, given the JSON document:
                    </preamble>
                    <artwork>
<![CDATA[
                      {
                         "foo": ["bar", "baz", "biz"],
                         "highly": {
                            "nested": {
                               "objects": true
                            }
                         }
                      }
]]>
                    </artwork>
                </figure>
                <figure align="center">
                    <preamble>
                        Starting from the value "baz" (inside "foo"), the following JSON
                                strings evaluate to the accompanying values:
                    </preamble>
                    <artwork>
<![CDATA[
                  "0"                         "baz"
                  "1/0"                       "bar"
                  "0-1"                       "bar"
                  "2/highly/nested/objects"   true
                  "0#"                        1
                  "0+1#"                      2
                  "1#"                        "foo"
]]>
                    </artwork>
                </figure>
                <figure align="center">
                    <preamble>
                        Starting from the value {"objects":true} (corresponding to the member
                              key "nested"), the following JSON strings evaluate to the
                                                accompanying values:
                    </preamble>
                    <artwork>
<![CDATA[
                "0/objects"                 true
                "1/nested/objects"          true
                "2/foo/0"                   "bar"
                "0#"                        "nested"
                "1#"                        "highly"
]]>
                    </artwork>
                </figure>
            </section>
        </section>

        <section title="Non-use in URI Fragment Identifiers">
            <t>
                Unlike a JSON Pointer, a Relative JSON Pointer can not be used in a
                URI fragment identifier.  Such fragments specify exact positions
                within a document, and therefore Relative JSON Pointers are not
                suitable.
            </t>
        </section>

        <section title="Error Handling">
            <t>
                In the event of an error condition, evaluation of the JSON Pointer
                fails to complete.
            </t>
            <t>
                Evaluation may fail due to invalid syntax, or referencing a non-
                existent value.  This specification does not define how errors are
                handled.  An application of JSON Relative Pointer SHOULD specify the
                impact and handling of each type of error.
            </t>
        </section>

        <section title="Relationship to JSON Pointer">
            <t>
                Relative JSON Pointers are intended as a companion to JSON Pointers.
                Applications MUST specify the use of each syntax separately.
                Defining either JSON Pointer or Relative JSON Pointer as an acceptable
                syntax does not imply that the other syntax is also acceptable.
            </t>
        </section>

        <section title="Acknowledgements">
            <t>
                The language and structure of this specification are based heavily on
                <xref target="RFC6901"/>, sometimes quoting it outright.
            </t>
            <t>
                This draft remains primarily as written and published by Geraint Luff,
                with only minor subsequent alterations under new editorship.
            </t>
        </section>

        <section title="Security Considerations">
            <t>
                Evaluation of a given Relative JSON Pointer is not guaranteed to
                reference an actual JSON value.  Applications using Relative JSON
                Pointer should anticipate this situation by defining how a pointer
                that does not resolve ought to be handled.
            </t>
            <t>
                As part of processing, a composite data structure may be assembled
                from multiple JSON documents (in part or in full).  In such cases,
                applications SHOULD ensure that a Relative JSON Pointer does not
                evaluate to a value outside the document for which is was written.
            </t>
            <t>
                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.
            </t>
        </section>

        <section title="IANA Considerations">
            <t>
                This document has no IANA actions.
            </t>
        </section>
    </middle>

    <back>
        <!-- References Section -->
        <references title="Normative References">
            &RFC2119;
            &RFC6901;
        </references>

        <references title="Informative References">
            &RFC8259;
        </references>

        <section title="ChangeLog">
            <t>
                <cref>This section to be removed before leaving Internet-Draft status.</cref>
            </t>
            <t>
                <list style="hanging">
                    <t hangText="draft-hha-relative-json-pointer-00">
                        <list style="symbols">
                            <t>Fix ABNF omission for using # with index manipulation</t>
                            <t>Clarify handling of leading "0"</t>
                        </list>
                    </t>
                    <t hangText="draft-bhutton-relative-json-pointer-00">
                        <list style="symbols">
                            <t>Add array forward and backward index manipulation</t>
                        </list>
                    </t>
                    <t hangText="draft-handrews-relative-json-pointer-02">
                        <list style="symbols">
                            <t>Update to the latest JSON RFC</t>
                        </list>
                    </t>
                    <t hangText="draft-handrews-relative-json-pointer-01">
                        <list style="symbols">
                            <t>The initial number is "non-negative", not "positive"</t>
                        </list>
                    </t>
                    <t hangText="draft-handrews-relative-json-pointer-00">
                        <list style="symbols">
                            <t>Revived draft with identical wording and structure.</t>
                            <t>Clarified how to use alongside JSON Pointer.</t>
                        </list>
                    </t>
                    <t hangText="draft-luff-relative-json-pointer-00">
                        <list style="symbols">
                            <t>Initial draft.</t>
                        </list>
                    </t>
                </list>
            </t>
        </section>
    </back>
</rfc>
