Skip to main content
This page is documentation for a DEVELOPMENT / PRE-RELEASE version. Switch to stable version
Ctrl+K

NetworkX User Survey 2023 🎉 Fill out the survey to tell us about your ideas, complaints, praises of NetworkX!

Logo image

Site Navigation

  • Install
  • Tutorial
  • Reference
  • Gallery
  • Developer
  • Releases
  • Guides
    • devel (latest)
    • current (stable)

Site Navigation

  • Install
  • Tutorial
  • Reference
  • Gallery
  • Developer
  • Releases
  • Guides
    • devel (latest)
    • current (stable)

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
    • Edge List
    • GEXF
    • GML
    • GraphML
    • JSON
    • LEDA
    • SparseGraph6
    • Pajek
    • Matrix Market
    • Network Text
  • Drawing
  • Randomness
  • Exceptions
  • Utilities
  • Glossary
  • Reference
  • JSON
  • node_link_data

node_link_data#

node_link_data(G, attrs=None, *, source='source', target='target', name='id', key='key', link='links')[source]#

Returns data in node-link format that is suitable for JSON serialization and use in Javascript documents.

Parameters:
GNetworkX graph
attrsdict

A dictionary that contains five keys ‘source’, ‘target’, ‘name’, ‘key’ and ‘link’. The corresponding values provide the attribute names for storing NetworkX-internal graph data. The values should be unique. Default value:

dict(source='source', target='target', name='id',
     key='key', link='links')

If some user-defined graph data use these attribute names as data keys, they may be silently dropped.

Deprecated since version 2.8.6: The attrs keyword argument will be replaced with source, target, name, key and link. in networkx 3.2

If the attrs keyword and the new keywords are both used in a single function call (not recommended) the attrs keyword argument will take precedence.

The values of the keywords must be unique.

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.

linkstring

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

Returns:
datadict

A dictionary with node-link formatted data.

Raises:
NetworkXError

If the values of ‘source’, ‘target’ and ‘key’ are not unique.

See also

node_link_graph, adjacency_data, tree_data

Notes

Graph, node, and link attributes are stored in this format. Note that attribute keys will be converted to strings in order to comply with JSON.

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

>>> G = nx.Graph([("A", "B")])
>>> data1 = nx.node_link_data(G)
>>> data1
{'directed': False, 'multigraph': False, 'graph': {}, 'nodes': [{'id': 'A'}, {'id': 'B'}], 'links': [{'source': 'A', 'target': 'B'}]}

To serialize with JSON

>>> import json
>>> s1 = json.dumps(data1)
>>> s1
'{"directed": false, "multigraph": false, "graph": {}, "nodes": [{"id": "A"}, {"id": "B"}], "links": [{"source": "A", "target": "B"}]}'

A graph can also be serialized by passing node_link_data as an encoder function. The two methods are equivalent.

>>> s1 = json.dumps(G, default=nx.node_link_data)
>>> s1
'{"directed": false, "multigraph": false, "graph": {}, "nodes": [{"id": "A"}, {"id": "B"}], "links": [{"source": "A", "target": "B"}]}'

The attribute names for storing NetworkX-internal graph data can be specified as keyword options.

>>> H = nx.gn_graph(2)
>>> data2 = nx.node_link_data(H, link="edges", source="from", target="to")
>>> data2
{'directed': True, 'multigraph': False, 'graph': {}, 'nodes': [{'id': 0}, {'id': 1}], 'edges': [{'from': 1, 'to': 0}]}
Ctrl+K
On this page
  • node_link_data()

© Copyright 2004-2023, NetworkX Developers.

Created using Sphinx 6.1.3.

Built with the PyData Sphinx Theme 0.13.1.