<｜begin▁of▁sentence｜>### Instruction:
Using NetworkX, how would you generate and represent a random tree with 5 nodes as a JSON object, including 'id' and 'label' for each node, as well as 'source', 'target', and 'label' for each edge?
### Response:
import networkx as nx
import json

# Generate a random tree with 5 nodes
T = nx.generators.trees.random_tree(5)

# Create JSON object
json_graph = {
  'graph nodes': [{'id': n, 'label': str(n)} for n in T.nodes()],
  'graph edges': [{'source': u, 'target': v, 'label': str(u)+'-'+str(v)} for u, v in T.edges()]
}

# Convert to JSON formatted string and print
json_graph_str = json.dumps(json_graph, indent=2)
print(json_graph_str)
<|EOT|>
