def recursive_node_to_dict(node):
    result = {'id': node.pk, 'name': node.name}
children = [recursive_node_to_dict(c) for c in node.get_children()]
if children:
    result['children'] = children
return result
root_nodes = cache_tree_children(Node.objects.all())
dicts = []
for n in root_nodes:
    dicts.append(recursive_node_to_dict(n))
print(json.dumps(dicts, indent=4))
fastest way to create JSON to reflect a tree structure in Python / Django using mptt