package jsont

  1. Overview
  2. Docs

JSON paths.

Paths are used for keeping track of erroring contexts and for specifying query and update locations.

Indices

type index =
  1. | Mem of string node
    (*

    Indexes the value of the member n of an object.

    *)
  2. | Nth of int node
    (*

    Indexes the value of the nth element of an array.

    *)

The type for indexing operations on JSON values.

val pp_index : index fmt

pp_index formats indexes.

val pp_index_trace : index fmt

pp_index formats indexes and their location.

Paths

type t

The type for paths, a sequence of indexing operations.

val root : t

root is the root path.

val is_root : t -> bool

is_root p is true iff p is the root path.

val nth : ?meta:Meta.t -> int -> t -> t

nth n p indexes the array indexed by p at index n.

val mem : ?meta:Meta.t -> string -> t -> t

mem n p indexes the object indexed by p at member n.

val rev_indices : t -> index list

rev_indices p are the indices of p in reverse order, the last indexing operation appears first.

val of_string : string -> (t, string) Stdlib.result

of_string s parses a path according to the path syntax.

val pp : t fmt

pp formats paths.

val pp_trace : t fmt

pp_trace formats paths as a stack trace, if not empty.

Path syntax

Path provide a way for end users to address JSON and edit locations.

A path is a sequence of member and list indexing operations. Applying the path to a JSON value leads to either a JSON value, or nothing if one of the indices does not exist, or an error if ones tries to index a non-indexable value.

Here are a few examples of paths.

{
  "ocaml": {
    "libs": ["jsont", "brr", "cmdliner"]
  }
}
ocaml.libs        # value of member "libs" of member "ocaml"
ocaml.libs.[0]    # first element of member "libs" of member "ocaml"

More formally a path is a . seperated list of indices. An index is written [i]. i can a zero-based list index. Or i can be an object member name n. If there is no ambiguity, the surrounding brackets can be dropped.

Notes.

  • The syntax has no form of quoting at the moment this means key names can't contain, [, ], or start with a number.
  • It would be nice to be able to drop the dots in order to be compatible with JSONPath syntax.
  • Reintroduce and implement negative indices (they are parsed).
OCaml

Innovation. Community. Security.