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.

@michael.hunger and @gg_lionkhan and @D_C any solution on above issue, Anything work for you. I am looking for similar solution.