Python Json Pointer
Python Json Pointer
Release 2.0
Stefan Kögl
1 Tutorial 3
i
ii
python-json-pointer Documentation, Release 2.0
python-json-pointer is a Python library for resolving JSON pointers (RFC 6901). Python 2.7, 3.4+ and PyPy are
supported.
Contents
Contents 1
python-json-pointer Documentation, Release 2.0
2 Contents
CHAPTER 1
Tutorial
Please refer to RFC 6901 for the exact pointer syntax. jsonpointer has two interfaces. The resolve_pointer
method is basically a deep get.
>>> from jsonpointer import resolve_pointer
>>> obj = {"foo": {"anArray": [ {"prop": 44}], "another prop": {"baz": "A string" }}}
True
The set_pointer method allows modifying a portion of an object using JSON pointer notation:
>>> from jsonpointer import set_pointer
>>> obj = {"foo": {"anArray": [ {"prop": 44}], "another prop": {"baz": "A string" }}}
>>> obj
{'foo': {'another prop': {'baz': 'A string'}, 'anArray': [{'prop': 55}]}}
3
python-json-pointer Documentation, Release 2.0
By default set_pointer modifies the original object. Pass inplace=False to create a copy and modify the
copy instead:
>>> obj
{'foo': {'another prop': {'baz': 'A string'}, 'anArray': [{'prop': 44}]}}
The JsonPointer class wraps a (string) path and can be used to access the same path on several objects.
4 Chapter 1. Tutorial
CHAPTER 2
5
python-json-pointer Documentation, Release 2.0
walk(doc, part)
Walks one step in doc and returns the referenced part
exception jsonpointer.JsonPointerException
jsonpointer.pairwise(iterable)
Transforms a list to a list of tuples of adjacent items
s -> (s0,s1), (s1,s2), (s2, s3), . . .
>>> list(pairwise([]))
[]
>>> list(pairwise([1]))
[]
>>> obj = {'foo': {'anArray': [ {'prop': 44}], 'another prop': {'baz': 'A string'
˓→}}, 'a%20b': 1, 'c d': 2}
True
>>> obj = {'foo': {'anArray': [ {'prop': 44}], 'another prop': {'baz': 'A string'
˓→}}}
True
True
7
python-json-pointer Documentation, Release 2.0
The JSON pointer package also installs a jsonpointer commandline utility that can be used to resolve a JSON
pointers on JSON files.
The program has the following usage
usage: jsonpointer [-h] [--indent INDENT] [-v] POINTER FILE [FILE ...]
positional arguments:
POINTER File containing a JSON pointer expression
FILE Files for which the pointer should be resolved
optional arguments:
-h, --help show this help message and exit
--indent INDENT Indent output by n spaces
-v, --version show program's version number and exit
3.1 Example
$ cat b.json
{ "a": {"b": [1, 3, 4]}, "b": 1 }
9
python-json-pointer Documentation, Release 2.0
• genindex
• modindex
• search
11
python-json-pointer Documentation, Release 2.0
j
jsonpointer, 5
13
python-json-pointer Documentation, Release 2.0
C
contains() (jsonpointer.JsonPointer method), 5
E
EndOfList (class in jsonpointer), 5
F
from_parts() (jsonpointer.JsonPointer class method), 5
G
get() (jsonpointer.JsonPointer method), 5
get_part() (jsonpointer.JsonPointer method), 5
J
JsonPointer (class in jsonpointer), 5
jsonpointer (module), 5
JsonPointerException, 6
P
pairwise() (in module jsonpointer), 6
path (jsonpointer.JsonPointer attribute), 5
R
resolve() (jsonpointer.JsonPointer method), 5
resolve_pointer() (in module jsonpointer), 6
S
set() (jsonpointer.JsonPointer method), 5
set_pointer() (in module jsonpointer), 7
T
to_last() (jsonpointer.JsonPointer method), 5
W
walk() (jsonpointer.JsonPointer method), 5
15