The jsonpointer module¶
Identify specific nodes in a JSON document (RFC 6901)
-
class
jsonpointer.EndOfList(list_)¶ Result of accessing element “-” of a list
-
class
jsonpointer.JsonPointer(pointer)¶ A JSON Pointer that can reference parts of an JSON document
-
contains(ptr)¶ Returns True if self contains the given ptr
-
classmethod
from_parts(parts)¶ Constructs a JsonPointer from a list of (unescaped) paths
>>> JsonPointer.from_parts(['a', '~', '/', 0]).path == '/a/~0/~1/0' True
-
get(doc, default=<object object>)¶ Resolves the pointer against doc and returns the referenced object
-
get_part(doc, part)¶ Returns the next step in the correct type
-
path¶ Returns the string representation of the pointer
>>> ptr = JsonPointer('/~0/0/~1').path == '/~0/0/~1'
-
resolve(doc, default=<object object>)¶ Resolves the pointer against doc and returns the referenced object
-
set(doc, value, inplace=True)¶ Resolve the pointer against the doc and replace the target with value.
-
to_last(doc)¶ Resolves ptr until the last step, returns (sub-doc, last-step)
-
walk(doc, part)¶ Walks one step in doc and returns the referenced part
-
-
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])) []
>>> list(pairwise([1, 2, 3, 4])) [(1, 2), (2, 3), (3, 4)]
-
jsonpointer.resolve_pointer(doc, pointer, default=<object object>)¶ Resolves pointer against doc and returns the referenced object
>>> 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
-
jsonpointer.set_pointer(doc, pointer, value, inplace=True)¶ Resolves pointer against doc and sets the value of the target within doc.
With inplace set to true, doc is modified as long as pointer is not the root.
>>> obj = {'foo': {'anArray': [ {'prop': 44}], 'another prop': {'baz': 'A string' }}}
>>> set_pointer(obj, '/foo/anArray/0/prop', 55) == {'foo': {'another prop': {'baz': 'A string'}, 'anArray': [{'prop': 55}]}} True
>>> set_pointer(obj, '/foo/yet%20another%20prop', 'added prop') == {'foo': {'another prop': {'baz': 'A string'}, 'yet another prop': 'added prop', 'anArray': [{'prop': 55}]}} True