Skip to content

JSON Paths

EnderTurret edited this page Nov 7, 2024 · 1 revision

Json paths are used to identify an element in a json document. They are defined by the RFC 6901 specification (although they're called 'JSON pointers' there.) For example, here are some json paths:

// the path "" points here
{
  "a": { // the path "/a" points here
    "b": [ // the path "/a/b" points here
      7,
      2 // the path "/a/b/1" points here
    ]
  }
}

Paths are made up of components separated by forward slashes. For example, the path /a/b/1 is made of the components "", "a", "b", and "1". Each component identifies a particular sub-element.

Path components can be:

  • a number, referring to either the corresponding index in an array or the value associated with the corresponding key in an object
  • a single dash, referring to after the end of an array (only in add patches), otherwise in objects it refers to the value associated with -
  • anything else, referring to the value associated with the corresponding key in an object

If a component cannot find its corresponding element, or if the parent element is not the correct type, then an error is raised.

Escaping

Since / has a special meaning (delimiting path components), there is a way to 'escape' the slashes by using tildes:

Escape sequence Meaning
~0 ~
~1 /

Clone this wiki locally