I have been using neo4j-driver in node-app to retrieve neo4j query results. I want to get this result in graph json (nodes and links) format to populate it into d3 like this: https://www.jsonkeeper.com/b/5ZYS
But the result I get is in this format: https://www.jsonkeeper.com/b/FMJ1
I tried a few solutions to achieve this but no one was able to implement it perfectly. Can you guide me how to implement it?
This is my controller in my Node Express application:
import neo4j from 'neo4j-driver'; import { NEO4J_PASSWORD, NEO4J_URL, NEO4J_USERNAME, } from '../../constants/index.js'; export const runMatchQuery = async (req, res) => { const query = req?.body?.query; if (query?.toLowerCase()?.includes('match')) { const driver = neo4j.driver( NEO4J_URL, neo4j.auth.basic(NEO4J_USERNAME, NEO4J_PASSWORD) ); const session = driver.session(); try { const result = await session.run(query); return res.status(200).json(result); } catch (error) { console.log(error); return res.status(500).json({ message: error.message }); } finally { await session.close(); await driver.close(); } } else { return res.send(405).json({ message: 'Query not permitted.' }); } };
P粉6681137682024-04-03 16:54:19
You can stream JSON to the output of node and relationship lists.
It's not in the same format as yours, but it's very close to a node in a list and a relationship in another list.
MATCH (n:MyNode)-[r:MY_REL]-() WITH COLLECT(n) as mynodes, COLLECT(r) as myrels CALL apoc.export.json.data(mynodes, myrels, null, {stream:True, jsonFormat: "JSON", writeNodeProperties:False}) YIELD data RETURN data
For more information about JSON output options, please visit: https://neo4j.com /labs/apoc/4.1/export/json/