HowTo JSONPointer - RFC6901

Pointer Syntax

JSON String Representation

See also

See RFC6901 section “5. JSON String Representation”

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
{
   "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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
""          // 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

URI Fragment Identifier Representation

See also

See RFC6901 section “6. URI Fragment Identifier Representation”

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
{
   "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
}

Given the same example document as above, the following URI fragment identifiers evaluate to the accompanying values:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#            // 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

Evaluate Nodes, Keys, and Values

get_node_and_child

Gets the parent node and the node of a given pointer. The child is the value of the actual pointed node. The data:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# -*- coding:utf-8   -*-
from jsondata.jsondata import JSONData
from jsondata.jsonpointer import JSONPointer

jsondata = JSONData(
    {
        "a": 10,
        "b": 11,
        "c": {
            "x": 20,
            "y": 21,
            "z": [
                {"r": 30},
                {"o": 31}
            ]
        }
    }
    )

x0,x1  = JSONPointer("/c/z/0").get_node_and_child(jsondata)

evaluates to:

1
2
x0 = [{'r': 30}, {'o': 31}]  # /c/z
x1 = {'r': 30}               # /c/z/0

get_node_and_key

Gets the parent node and the key of a given pointer. The key is the last item of the path, pointing to the node. The data:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# -*- coding:utf-8   -*-
from jsondata.jsondata import JSONData
from jsondata.jsonpointer import JSONPointer

jsondata = JSONData(
    {
        "a": 10,
        "b": 11,
        "c": {
            "x": 20,
            "y": 21,
            "z": [
                {"r": 30},
                {"o": 31}
            ]
        }
    }
    )

x0,x1  = JSONPointer("/c/z/0").get_node_and_key(jsondata)

evaluates to:

1
2
x0 = [{'r': 30}, {'o': 31}]  # /c/z
x1 = 0                       # 0

get_node_value

Gets the value of the pointed node, which is basically the same as the result of get_node. The data:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# -*- coding:utf-8   -*-
from jsondata.jsondata import JSONData
from jsondata.jsonpointer import JSONPointer

jsondata = JSONData(
    {
        "a": 10,
        "b": 11,
        "c": {
            "x": 20,
            "y": 21,
            "z": [
                {"r": 30},
                {"o": 31}
            ]
        }
    }
    )

x0,x1  = JSONPointer("/c/z/0").get_node_value(jsondata)

evaluates to:

1
x = {'r': 30}   # /c/z/0

get_node_exist

Gets the path splitted into it’s existing component, and the non-existent part of the path. When the node exists, the latter is empty. The data:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# -*- coding:utf-8   -*-
from jsondata.jsondata import JSONData
from jsondata.jsonpointer import JSONPointer

jsondata = JSONData(
    {
        "a": 10,
        "b": 11,
        "c": {
            "x": 20,
            "y": 21,
            "z": [
                {"r": 30},
                {"o": 31}
            ]
        }
    }
    )

x0,x1  = JSONPointer("/c/z/0").get_node_value(jsondata)

evaluates to:

1
x = [{'r': 30}, None]

While the call:

1
x  = JSONPointer("/c/z/0/y").get_node_value(jsondata)

evaluates to:

1
x = [{'r': 30}, ['y']]

Iterate Paths

Iterate Path Items

The method iter_path iterates the path parts of the JSONPointer itself.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# -*- coding:utf-8   -*-
from __future__ import absolute_import
from __future__ import print_function

from jsondata.jsondata import JSONData
from jsondata.jsonpointer import JSONPointer

jsondata = JSONData(
    {'a': {'b': [{'c': 2, 'd': 4, 'f': 3}]}}
    )

jp = JSONPointer('/a/b/0/c')


for jpi in jp.iter_path():
    print(jpi)

with the resulting display

1
2
3
4
a
b
0
c

The following example verifies the path items for presence by using the data.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# -*- coding:utf-8   -*-
from __future__ import absolute_import
from __future__ import print_function

from jsondata.jsondata import JSONData
from jsondata.jsonpointer import JSONPointer

jsondata = JSONData(
    {'a': {'b': [{'c': 2, 'd': 4, 'f': 3}]}}
    )

jp = JSONPointer('/a/b/1/c')


for jpi in jp.iter_path(jsondata):
    print(jpi)

Resulting for ‘/a/b/1/c‘ in the error

1
jsondata.JSONPointerError: ERROR::Node(2):1 of /a/b/1/c:list index out of range

Iterate Sub Paths

The method iter_path_subpaths iterates the sub paths resulting from cumulated the path items.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# -*- coding:utf-8   -*-
from __future__ import absolute_import
from __future__ import print_function

from jsondata.jsondata import JSONData
from jsondata.jsonpointer import JSONPointer

jsondata = JSONData(
    {'a': {'b': [{'c': 2, 'd': 4, 'f': 3}]}}
    )

jp = JSONPointer('/a/b/0/c')
for jpi in jp.iter_path_subpaths(jsondata):
    print(jpi)

Resulting for ‘/a/b/0/c‘ in

1
2
3
4
['a']
['a', 'b']
['a', 'b', 0]
['a', 'b', 0, 'c']

Iterate Path Nodes

The method iter_path_nodes iterates the nodes resulting from the path items of the JSONPointer.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# -*- coding:utf-8   -*-
from __future__ import absolute_import
from __future__ import print_function

from jsondata.jsondata import JSONData
from jsondata.jsonpointer import JSONPointer

jsondata = JSONData(
    {'a': {'b': [{'c': 2, 'd': 4, 'f': 3}]}}
    )

jp = JSONPointer('/a/b/0/c')

for jpi in jp.iter_path_nodes(jsondata):
    print(jpi)

Resulting for ‘/a/b/0/c‘ in the display of the node contents of the cumulated subpaths

1
2
3
4
{'b': [{'c': 2, 'd': 4, 'f': 3}]}
[{'c': 2, 'd': 4, 'f': 3}]
{'c': 2, 'd': 4, 'f': 3}
2

Iterate Path Data

The method iter_path_subpathdata iterates the complete data resulting from the path items of the JSONPointer.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# -*- coding:utf-8   -*-
from __future__ import absolute_import
from __future__ import print_function

from jsondata.jsondata import JSONData
from jsondata.jsonpointer import JSONPointer

jsondata = JSONData(
    {'a': {'b': [{'c': 2, 'd': 4, 'f': 3}]}}
    )

jp = JSONPointer('/a/b/0/c')

for jpi in jp.iter_path_subpathdata(jsondata):
    print(jpi)

Resulting for ‘/a/b/0/c‘ in the display of the node contents of the cumulated subpaths consisting of the tuple

1
(<path-item>, <sub-path>, <node>)

with the output

1
2
3
4
('a', ['a'], {'b': [{'c': 2, 'd': 4, 'f': 3}]})
('b', ['a', 'b'], [{'c': 2, 'd': 4, 'f': 3}])
(0, ['a', 'b', 0], {'c': 2, 'd': 4, 'f': 3})
('c', ['a', 'b', 0, 'c'], 2)