Lilbeartr
Lilbeartr

Reputation: 55

networkx - Dump graph into JSON with weight

I try to dump the networkx graph into JSON by using node_link_data. I am able to do that but the link from JSON I got only has target and source.

My question is how do I get weight write to JSON together with the links.

Upvotes: 2

Views: 2344

Answers (1)

Vladimir S.
Vladimir S.

Reputation: 548

There's something wrong with how you set weight to an edge. Since you haven't provided any code, here's an example to get you started.

import networkx as nx
from networkx.readwrite import json_graph

G = nx.Graph([(1,2)])
G.add_edge(1,2, weight=5)

json_graph.node_link_data(G)

gives output

{'directed': False, 'graph': {},
'links': [{'source': 0, 'target': 1, 'weight': 5}],
'multigraph': False, 'nodes': [{'id': 1}, {'id': 2}]}

As you can see weight is there.

Upvotes: 2

Related Questions