JSON Importer

class anytree.importer.jsonimporter.JsonImporter(dictimporter=None, **kwargs)[source]

Bases: object

Import Tree from JSON.

The JSON is read and converted to a dictionary via dictimporter.

Keyword Arguments:
 
  • dictimporter – Dictionary Importer used (see DictImporter).
  • kwargs – All other arguments are passed to json.load/json.loads. See documentation for reference.
>>> from anytree.importer import JsonImporter
>>> from anytree import RenderTree
>>> importer = JsonImporter()
>>> data = '''
... {
...   "a": "root",
...   "children": [
...     {
...       "a": "sub0",
...       "children": [
...         {
...           "a": "sub0A",
...           "b": "foo"
...         },
...         {
...           "a": "sub0B"
...         }
...       ]
...     },
...     {
...       "a": "sub1"
...     }
...   ]
... }'''
>>> root = importer.import_(data)
>>> print(RenderTree(root))
AnyNode(a='root')
├── AnyNode(a='sub0')
│   ├── AnyNode(a='sub0A', b='foo')
│   └── AnyNode(a='sub0B')
└── AnyNode(a='sub1')
import_(data)[source]

Read JSON from data.

read(filehandle)[source]

Read JSON from filehandle.