Execute Cypher and get Json response from embedded db

44 Views Asked by At

I have a Spring Boot webapp with embedded neo4j and I want to execute custom Cypher and get a response in form of Json. Like that:

try (var tx = graphDatabaseService.beginTx()) {
  org.neo4j.graphdb.Result result = tx.execute(cypherQuery);
  String json = JsonSerializer.writeValueAsString(result);
}

I assume such serializer exists (which can take Result, Node, Relationship etc objects) because there is a org.neo4j.configuration.connectors.HttpConnector which enables Http endpoint which allows to do exactly what I need:

https://neo4j.com/docs/http-api/current/query/

{
  "results": [ {
    "columns": [ "p" ],
    "data": [ {
      "row": [ {
        "name": "Bob"
      } ],
      "meta": [ {
        "id": 8,
        "elementId": "4:0ea4a108-32c5-498c-99e7-95cc67ab5f7d:8",
        "type": "node",
        "deleted": false
      } ]
    ] }
  ] },
  {
    "columns": [ "n" ],
    "data": [ {
      "row": [ {
        "val": 1
      } ],
      "meta": [ {
        "id": 1,
        "elementId": "4:0ea4a108-32c5-498c-99e7-95cc67ab5f7d:1",
        "type": "node",
        "deleted": false
      } ]
    } ]
  } ],
  "errors": [ {
    "code": "Neo.ClientError.Statement.ArithmeticError",
    "message": "/ by zero"
  } ]
}

but HttpConnector doesn't work in embedded neo4j. But maybe I can use just serializer, not whole endpoint.

This question and answers show how it can be written manually, but probably such class should exist in org.neo4j.neo4j maven artifact already. I can't find it.

Another approach is to use apoc export json function but it requires enabling apoc plugin and there is no info on how to do it for embedded db.

0

There are 0 best solutions below