Skip to main content
Ctrl+K
NetworkX 3.5 documentation - Home NetworkX 3.5 documentation - Home
  • Install
  • Tutorial
  • Backends
  • Reference
  • Gallery
  • Developer
  • Releases
  • Guides
  • Home Page
  • GitHub
  • Install
  • Tutorial
  • Backends
  • Reference
  • Gallery
  • Developer
  • Releases
  • Guides
  • Home Page
  • GitHub

Section Navigation

  • Introduction
  • Graph types
  • Algorithms
  • Functions
  • Graph generators
  • Linear algebra
  • Converting to and from other data formats
  • Relabeling nodes
  • Reading and writing graphs
    • Adjacency List
    • Multiline Adjacency List
    • DOT
    • Edge List
    • GEXF
    • GML
    • GraphML
    • JSON
    • LEDA
    • SparseGraph6
    • Pajek
    • Matrix Market
    • Network Text
  • Drawing
  • Randomness
  • Exceptions
  • Utilities
  • Backends
  • Configs
  • Glossary
  • Reference
  • Reading and writing graphs
  • JSON
  • node_link_graph

node_link_graph#

node_link_graph(data, directed=False, multigraph=True, *, source='source', target='target', name='id', key='key', edges=None, nodes='nodes', link=None)[source]#

Returns graph from node-link data format.

Useful for de-serialization from JSON.

Parameters:
datadict

node-link formatted graph data

directedbool

If True, and direction not specified in data, return a directed graph.

multigraphbool

If True, and multigraph not specified in data, return a multigraph.

sourcestring

A string that provides the ‘source’ attribute name for storing NetworkX-internal graph data.

targetstring

A string that provides the ‘target’ attribute name for storing NetworkX-internal graph data.

namestring

A string that provides the ‘name’ attribute name for storing NetworkX-internal graph data.

keystring

A string that provides the ‘key’ attribute name for storing NetworkX-internal graph data.

edgesstring

A string that provides the ‘edges’ attribute name for storing NetworkX-internal graph data.

nodesstring

A string that provides the ‘nodes’ attribute name for storing NetworkX-internal graph data.

linkstring

Deprecated since version 3.4: The link argument is deprecated and will be removed in version 3.6. Use the edges keyword instead.

A string that provides the ‘edges’ attribute name for storing NetworkX-internal graph data.

Returns:
GNetworkX graph

A NetworkX graph object

See also

node_link_data, adjacency_data, tree_data

Notes

Attribute ‘key’ is only used for multigraphs.

To use node_link_data in conjunction with node_link_graph, the keyword names for the attributes must match.

Examples

Create data in node-link format by converting a graph.

>>> from pprint import pprint
>>> G = nx.Graph([("A", "B")])
>>> data = nx.node_link_data(G, edges="edges")
>>> pprint(data)
{'directed': False,
 'edges': [{'source': 'A', 'target': 'B'}],
 'graph': {},
 'multigraph': False,
 'nodes': [{'id': 'A'}, {'id': 'B'}]}

Revert data in node-link format to a graph.

>>> H = nx.node_link_graph(data, edges="edges")
>>> print(H.edges)
[('A', 'B')]

To serialize and deserialize a graph with JSON,

>>> import json
>>> d = json.dumps(nx.node_link_data(G, edges="edges"))
>>> H = nx.node_link_graph(json.loads(d), edges="edges")
>>> print(G.edges, H.edges)
[('A', 'B')] [('A', 'B')]
On this page
  • node_link_graph()

© Copyright 2004-2025, NetworkX Developers.

Created using Sphinx 8.2.3.

Built with the PyData Sphinx Theme 0.16.1.