···99Before diving in, it's worth understanding the difference between JSON
1010Pointer and JSON Path, as they serve different purposes:
11111212-{b JSON Pointer} (RFC 6901) is an {e indicator syntax} that specifies a
1212+{b JSON Pointer} ({{:https://datatracker.ietf.org/doc/html/rfc6901}RFC 6901}) is an {e indicator syntax} that specifies a
1313{e single location} within JSON data. It always identifies at most one
1414value.
1515···42424343{1 What is JSON Pointer?}
44444545-From RFC 6901, Section 1:
4545+From {{:https://datatracker.ietf.org/doc/html/rfc6901#section-1}RFC 6901, Section 1}:
46464747{i JSON Pointer defines a string syntax for identifying a specific value
4848within a JavaScript Object Notation (JSON) document.}
···80808181{1 Syntax: Reference Tokens}
82828383-RFC 6901, Section 3 defines the syntax:
8383+{{:https://datatracker.ietf.org/doc/html/rfc6901#section-3}RFC 6901, Section 3} defines the syntax:
84848585{i A JSON Pointer is a Unicode string containing a sequence of zero or more
8686reference tokens, each prefixed by a '/' (%x2F) character.}
···168168169169{1 Evaluation: Navigating JSON}
170170171171-Now we come to the heart of JSON Pointer: evaluation. RFC 6901, Section 4
171171+Now we come to the heart of JSON Pointer: evaluation. {{:https://datatracker.ietf.org/doc/html/rfc6901#section-4}RFC 6901, Section 4}
172172describes how a pointer is resolved against a JSON document:
173173174174{i Evaluation of a JSON Pointer begins with a reference to the root value
···176176the document. Each reference token in the JSON Pointer is evaluated
177177sequentially.}
178178179179-Let's use the example JSON document from RFC 6901, Section 5:
179179+Let's use the example JSON document from {{:https://datatracker.ietf.org/doc/html/rfc6901#section-5}RFC 6901, Section 5}:
180180181181{x@ocaml[
182182# let rfc_example = parse_json {|{
···326326327327{2 Array Index Rules}
328328329329-RFC 6901 has specific rules for array indices. Section 4 states:
329329+{{:https://datatracker.ietf.org/doc/html/rfc6901}RFC 6901} has specific rules for array indices. {{:https://datatracker.ietf.org/doc/html/rfc6901#section-4}Section 4} states:
330330331331{i characters comprised of digits [...] that represent an unsigned base-10
332332integer value, making the new referenced value the array element with
···354354355355{1 The End-of-Array Marker: [-] and Type Safety}
356356357357-RFC 6901, Section 4 introduces a special token:
357357+{{:https://datatracker.ietf.org/doc/html/rfc6901#section-4}RFC 6901, Section 4} introduces a special token:
358358359359{i exactly the single character "-", making the new referenced value the
360360(nonexistent) member after the last array element.}
361361362362This [-] marker is unique to JSON Pointer (JSON Path has no equivalent).
363363-It's primarily useful for JSON Patch operations (RFC 6902) to append
363363+It's primarily useful for JSON Patch operations ({{:https://datatracker.ietf.org/doc/html/rfc6902}RFC 6902}) to append
364364elements to arrays.
365365366366{2 Navigation vs Append Pointers}
···438438439439{1 Mutation Operations}
440440441441-While RFC 6901 defines JSON Pointer for read-only access, RFC 6902
441441+While {{:https://datatracker.ietf.org/doc/html/rfc6901}RFC 6901} defines JSON Pointer for read-only access, {{:https://datatracker.ietf.org/doc/html/rfc6902}RFC 6902}
442442(JSON Patch) uses JSON Pointer for modifications. The [json-pointer]
443443library provides these operations.
444444···566566567567{1:escaping Escaping Special Characters}
568568569569-RFC 6901, Section 3 explains the escaping rules:
569569+{{:https://datatracker.ietf.org/doc/html/rfc6901#section-3}RFC 6901, Section 3} explains the escaping rules:
570570571571{i Because the characters '~' (%x7E) and '/' (%x2F) have special meanings
572572in JSON Pointer, '~' needs to be encoded as '~0' and '/' needs to be
···623623624624{2 The Order Matters!}
625625626626-RFC 6901, Section 4 is careful to specify the unescaping order:
626626+{{:https://datatracker.ietf.org/doc/html/rfc6901#section-4}RFC 6901, Section 4} is careful to specify the unescaping order:
627627628628{i Evaluation of each reference token begins by decoding any escaped
629629character sequence. This is performed by first transforming any
···646646647647{1 URI Fragment Encoding}
648648649649-JSON Pointers can be embedded in URIs. RFC 6901, Section 6 explains:
649649+JSON Pointers can be embedded in URIs. {{:https://datatracker.ietf.org/doc/html/rfc6901#section-6}RFC 6901, Section 6} explains:
650650651651{i A JSON Pointer can be represented in a URI fragment identifier by
652652encoding it into octets using UTF-8, while percent-encoding those
653653-characters not allowed by the fragment rule in RFC 3986.}
653653+characters not allowed by the fragment rule in {{:https://datatracker.ietf.org/doc/html/rfc3986}RFC 3986}.}
654654655655This adds percent-encoding on top of the [~0]/[~1] escaping:
656656···856856- : Jsont.json = {"tasks":["buy milk","call mom"]}
857857]x}
858858859859-This is useful for implementing JSON Patch ([RFC 6902]) where
859859+This is useful for implementing JSON Patch ({{:https://datatracker.ietf.org/doc/html/rfc6902}RFC 6902}) where
860860operations like ["add"] can target either existing positions or the
861861append marker. If you need to distinguish between pointer types at runtime,
862862use {!of_string_kind} which returns a polymorphic variant:
···870870871871{1 Summary}
872872873873-JSON Pointer (RFC 6901) provides a simple but powerful way to address
873873+JSON Pointer ({{:https://datatracker.ietf.org/doc/html/rfc6901}RFC 6901}) provides a simple but powerful way to address
874874values within JSON documents:
875875876876{ol
···878878{- {b Escaping}: Use [~0] for [~] and [~1] for [/] in tokens (handled automatically by the library)}
879879{- {b Evaluation}: Tokens navigate through objects (by key) and arrays (by index)}
880880{- {b URI Encoding}: Pointers can be percent-encoded for use in URIs}
881881-{- {b Mutations}: Combined with JSON Patch (RFC 6902), pointers enable structured updates}
881881+{- {b Mutations}: Combined with JSON Patch ({{:https://datatracker.ietf.org/doc/html/rfc6902}RFC 6902}), pointers enable structured updates}
882882{- {b Type Safety}: Phantom types ([nav t] vs [append t]) prevent misuse of append pointers with retrieval operations, while the [any] existential type allows ergonomic use with mutation operations}
883883}
884884