I have a networkx graph, and I would like to send a node (including its attributes) through a connection using json format. I know how to serialize the whole graph:
import networkx as nx
from networkx.readwrite import json_graph
G=nx.Graph()
G.add_node(1)
G.node[1]["name"]="alice"
G.add_node(2)
G.add_edge(1,2)
print json.dumps(json_graph.node_link_data(G))
However, I didn’t find a way to serialize a single node, something like
print json.dumps(json_graph.node_data(G.node[1]))
Is there a way to achieve this?
You could call json.dumps() on the node or a tuple of (node,data).
The same would work for edges. For example: