Cytoscape JS format JSON

Hi! I'm trying to render a graph in cytoscape JS which requires a format like this, using python server.

 elements: {
    nodes: [
      {
        data: { id: 'a' }
      },

      {
        data: { id: 'b' }
      }
    ],
    edges: [
      {
        data: { id: 'ab', source: 'a', target: 'b' }
      }
    ]
  },

For a simple graph I can convert it myself by walking through the results

As graphs get more complex, picking out the structures will end up getting convoluted to serialize the data in the right way.

I was thinking this should be a common requirement, maybe someone has a better way?

I was looking at the networkX cytoscape output source code as a reference.

Using subgraphs seems it would make all my queries twice as complex, I hope this isn't needed.

Can't you just use result.graph() and map the results into nodes and rels to be serialized?

https://neo4j.com/docs/api/python-driver/current/api.html#graph

Could you please elaborate more on your proposition? @michael.hunger
How to serialize this..
Please help.