Representing a graph in JSON

json-ld was made for this. It has a semi-steep learning curve, but it is a robust way to represent graph data in json.


Turns out there are standards that are trying to emerge for this sort of thing. I recently had to look these up for a project of my own. You might be interested in http://jsongraphformat.info/ for example, or one of the peer projects it references on its website. Goals include trying to represent in JSON anything you can represent in the DOT language (https://en.wikipedia.org/wiki/DOT_(graph_description_language)).


Since the DAG's edges hold data, they better have their own identifiers, just like the nodes. That is, the json representation should be composed of three components:

  1. Node records: mapping each node identifier to the node's data.
  2. Edge records: mapping each edge identifier to the edge's data.
  3. Adjacency lists: mapping each node identifier to an array of edge identifiers, each corresponds to an edge going out of the node.

    DAG = {
      "adjacency": {
        "a": ["1", "2"],
        "b": ["3"]
      },
      "nodes": {
        "a": {
          // data
        },
        "b": {
          // data
        },
        "c": {
          // data
        }
      },
      "edges": {
        "1": {
          "from": "a", "to": "b",
          "data": {
            // data
          }
        },
        "2": {
          "from": "a", "to": "b",
          "data": {
            // data
          }
        },
        "3": {
          "from": "b", "to": "c",
          "data": {
            // data
          }
        }
      }
    }