Home > Software design >  How to convert a JSON file to a graph with library networkx in Python?
How to convert a JSON file to a graph with library networkx in Python?

Time:03-25

I have a big JSON file and I want to convert it with networkx library to a graph. The JSON file contains so many nodes like:

{"data": [{ "d": 2, "source": "C", "target": "L" },...]}

I already opened the JSON file and I extracted each "data" dictionary inside the JSON file. However, I do not know how to use networkx to convert all of my source, target nodes with considering the attribute "d" to a graph.

import networkx as nx
import json

lst = list()
with open('json_file.json') as json_load:
    data = json.load(json_load)
    lst.extend( data["edges"])  
    d, src, tgt = [],[], [], [] 
    
    for elem in lst:
      d.append(elem["data"]["d"])
      src.append(elem["data"]["source"])
      tgt.append(elem["data"]["target"])

G = nx.Graph()      
G.add_edges_from([tuple(zip(src, tgt))])

When I wrote the last line for inserting the edges into the G graph, it did not really work. I had an error. I guess because I did not properly merge the source and target nodes together.

Also, I have another problem as well. I couldn’t figure out how to consider attribute "d" for each node in the graph as well.

CodePudding user response:

The argument passed to G.add_edges_from is not valid, specifically the result of zip-ping several iterables together is already an iterable of tuples, so only zip(src, tgt, d) is needed. Next, make sure that d is a list of dictionaries, as per function requirements.

Here's a reproducible example:

import networkx as nx

src = ["A", "B", "C"]
tgt = ["B", "C", "D"]
d = [{"weight": 2}, {"weight": 3}, {"weight": 4}]

G = nx.Graph()
G.add_edges_from(zip(src, tgt, d))
  • Related