Skip to main content
`Ctrl`+`K`

# tree_data

tree_data(G, root, ident='id', children='children')

  Returns data in tree format that is suitable for JSON serialization and use in JavaScript documents.

  Parameters:
    G NetworkX graph

      G must be an oriented tree

    root node

      The root of the tree

    ident string

      Attribute name for storing NetworkX-internal graph data. `ident` must have a different value than `children`. The default is ‘id’.

    children string

      Attribute name for storing NetworkX-internal graph data. `children` must have a different value than `ident`. The default is ‘children’.

  Returns:
    data dict

      A dictionary with node-link formatted data.

  Raises:
    NetworkXError

      If `children` and `ident` attributes are identical.

  See also

  `tree_graph`, `node_link_data`, `adjacency_data`

  Notes

  Node attributes are stored in this format but keys for attributes must be strings if you want to serialize with JSON.

  Graph and edge attributes are not stored.

  Examples

      >>> from networkx.readwrite import json_graph
      >>> G = nx.DiGraph([(1, 2)])
      >>> data = json_graph.tree_data(G, root=1)

  To serialize with json

      >>> import json
      >>> s = json.dumps(data)