2

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.

Lilbeartr
  • 55
  • 1
  • 8

1 Answers1

2

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.

Vladimir S.
  • 483
  • 6
  • 16
  • Oh I see. Thanks for the answer. I was working with csv data like [this thread](https://stackoverflow.com/questions/29572623/plot-networkx-graph-from-adjacency-matrix-in-csv-file). But my data is 0 to 1 and I only write edge when the value is more than 0.7. – Lilbeartr May 27 '17 at 12:21
  • I'm not sure if you still have a question. Is it how do I remove edges from a graph with weight <0.7? You can do it with a for loop. Or is it something else? – Vladimir S. May 28 '17 at 16:59