summaryrefslogtreecommitdiff
path: root/doc/tutorial.rst
blob: 4cdb75a1d9726796e51b260dcb7b48241b01d7a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Tutorial
========

Please refer to `RFC 6901 <http://tools.ietf.org/html/rfc6901>`_ for the exact
pointer syntax. ``jsonpointer`` has two interfaces. The ``resolve_pointer``
method is basically a deep ``get``.

.. code-block:: python

    >>> import jsonpointer
    >>> obj = {"foo": {"anArray": [ {"prop": 44}], "another prop": {"baz": "A string" }}}

    >>> resolve_pointer(obj, '') == obj
    True

    >>> resolve_pointer(obj, '/foo') == obj['foo']
    True

    >>> resolve_pointer(obj, '/foo/another%20prop') == obj['foo']['another prop']
    True

    >>> resolve_pointer(obj, '/foo/another%20prop/baz') == obj['foo']['another prop']['baz']
    True

    >>> resolve_pointer(obj, '/foo/anArray/0') == obj['foo']['anArray'][0]
    True

    >>> resolve_pointer(obj, '/some/path', None) == None
    True


The ``JsonPointer`` class wraps a (string) path and can be used to access the
same path on several objects.

.. code-block:: python

    >>> import jsonpointer

    >>> pointer = jsonpointer.JsonPointer('/foo/1')

    >>> obj1 = {'foo': ['a', 'b', 'c']}
    >>> pointer.resolve(obj1)
    'b'

    >>> obj2 = {'foo': {'0': 1, '1': 10, '2': 100}}
    >>> pointer.resolve(obj2)
    10