I have done some network generation and analysis in Python NetworkX. I do most of my visualizations with D3, but for publications I would like to generate some graph plots with Mathematica as well as perform some additional analyses. My question is: How do I import the JSON exported from NetworkX and read it in as a Mathematica graph?

Importing the JSON itself is easy. Manipulating the resulting rule sets from the JSON dictionaries into what is needed by Mathematica is also certainly possible, and certainly complicated. My expectation is that somebody has already done this and has (at least partial) code to share. There is already a question about importing specific elements from the JSON which will likely be useful to collect data and assign it to the appropriate Mathematica command.

Here is an example of going in the opposite direction: from Mathematica to the NetworkX-style JSON, so people are working in the neighborhood of this functionality.

For a specific example you can download a test JSON of the Les Miserables characters here. The question is how to import that and render it into a Mathematica graph with the properties assigned to the proper nodes and the edges connecting the proper nodes. In miserables.json there is a node property called "group" and I would like to color the nodes by that property and label them by the "name" property. Any layout is fine.

Secondly I would like to be able generate data lists from the node (or edge) properties (to generate plots like histograms). This can be done from the resulting Mathematica graph or directly from the imported JSON...whichever is easier...but it's actually better for me to do it directly from the imported JSON. This second part is really just a question about rule manipulation: how to go through a JSON structured the way NetworkX makes them and collect all and only a particular node property into a Mathematica list of values.

  • $\begingroup$ For completeness, could you please include a picture of what the graph looks like in D3? $\endgroup$ Jun 16, 2016 at 11:39

1 Answer 1


Here's a solution using your example file.

Import as RawJSON because this gives us much easier to manipulate associations.

json = Import[
  "https://raw.githubusercontent.com/d3/d3-plugins/master/graph/data/miserables.json", "RawJSON"]

After inspecting the format manually, it's easy to extract vertices:

vertices = json[["nodes", All, "name"]]

Links are stored as 0-based vertex indices. We extract these and convert to 1-based indices by adding 1.

edgesAsIndexPairs = 1 + Values /@ json[["links", All, {"source", "target"}]];

Convert to proper edges in terms of vertices:

edges = DirectedEdge @@ vertices[[#]] & /@ edgesAsIndexPairs;

Get a graph:

g = Graph[vertices, edges]

I know there are weird things in here such as isolated vertices and self-loops, so maybe I misinterpreted that format. If this is the case, correct me. There are indeed entries with the same source and target.

Data list for edge value:

json[["links", All, "value"]]

Data list for vertex group:

groups = json[["nodes", All, "group"]]

Colour graph:

Graph[g, VertexStyle -> 
  Thread[VertexList[g] -> (ColorData[97] /@ (groups + 1))], 
 VertexSize -> 1.5]

enter image description here

Community graph plot:

classes = Pick[VertexList[g], groups, #] & /@ Union[groups];

CommunityGraphPlot[g, classes]

enter image description here

  • $\begingroup$ I'm finally getting to this part of the project. Thank you for the thorough answer, but I just tried this with v10.1 and it failed to import: "The Import element "RawJSON" is not present when importing as JSON". And I get the same error when I load my local file. Loading as a normal JSON doesn't fail for either, but then nothing else works, e.g.: "Part specification nodes is not applicable". $\endgroup$ Jul 2, 2016 at 15:13
  • $\begingroup$ @AaronBramson RawJSON must have been introduces in 10.2 or 10.3 then. All it does is it imports JSON as nested associations instead of nested rules. But nested rules from be converted to associations too, then the rest of the answer is the same. $\endgroup$
    – Szabolcs
    Jul 2, 2016 at 15:31
  • $\begingroup$ Indeed, RawJSON support was added with version 10.2. I'll upgrade and try again. $\endgroup$ Jul 2, 2016 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.