add JSON helpers

This commit is contained in:
smurail 2015-05-06 11:23:32 +02:00 committed by Romain Bignon
commit 62adf86daa
2 changed files with 55 additions and 1 deletions

View file

@ -412,6 +412,18 @@ class JsonPage(Page):
def data(self):
return self.response.text
def get(self, path):
node = self.doc
for name in filter(None, path.strip('.').split('.')):
node = node.get(name)
if node is None:
break
return node
def path(self, path):
from weboob.tools.json import mini_jsonpath
return mini_jsonpath(self.doc, path)
def build_doc(self, text):
from weboob.tools.json import json
return json.loads(text)

View file

@ -20,7 +20,7 @@
# because we don't want to import this file by "import json"
from __future__ import absolute_import
__all__ = ['json']
__all__ = ['json', 'mini_jsonpath']
try:
# try simplejson first because it is faster
@ -28,3 +28,45 @@ try:
except ImportError:
# Python 2.6+ has a module similar to simplejson
import json
def mini_jsonpath(node, path):
"""
Evaluates a dot separated path against JSON data. Path can contains
star wilcards. Always returns a generator.
Relates to http://goessner.net/articles/JsonPath/ but in a really basic
and simpler form.
>>> list(mini_jsonpath({"x": 95, "y": 77, "z": 68}, 'y'))
[77]
>>> list(mini_jsonpath({"x": {"y": {"z": "nested"}}}, 'x.y.z'))
['nested']
>>> list(mini_jsonpath('{"data": [{"x": "foo", "y": 13}, {"x": "bar", "y": 42}, {"x": "baz", "y": 128}]}', 'data.*.y'))
[13, 42, 128]
"""
def iterkeys(i):
return range(len(i)) if type(i) is list else i.iterkeys()
def cut(s):
p = s.split('.', 1) if s else [None]
return p + [None] if len(p) == 1 else p
if isinstance(node, basestring):
node = json.loads(node)
queue = [(node, cut(path))]
while queue:
node, (name, rest) = queue.pop(0)
if name is None:
yield node
continue
elif type(node) not in (dict, list):
continue
if name == '*':
keys = iterkeys(node)
else:
keys = [int(name) if type(node) is list else name]
for k in keys:
queue.append((node[k], cut(rest)))