javaneo4jneo4j-bolt

Neo4j Java bolt driver: how to convert the result to Json?


I am using the Java Bolt driver (1.0.1) and I am wondering there is a way to convert the result to Json (possibly the same as in the REST api)?

I tried to use gson in this way:

Result r = null;
try ( Transaction tx = graphDb.beginTx() )
{
    r = graphDb.execute("MATCH...");
    tx.success();
} catch {...}

new Gson().toJson(result);

but what I get is:

java.lang.StackOverflowError
    at com.google.gson.internal.$Gson$Types.canonicalize($Gson$Types.java:98)
    at com.google.gson.reflect.TypeToken.<init>(TypeToken.java:72)
    etc...

Solution

  • The API you show is not the Bolt-Driver, it's the embedded Java-API.

    In the bolt-driver you can do

    Driver driver = GraphDatabase.driver( "bolt://localhost", AuthTokens.basic( "neo4j", "neo4j" ) );
    Session session = driver.session();
    
    StatementResult result = session.run( "MATCH (a:Person) WHERE a.name = 'Arthur' RETURN a.name AS name, a.title AS title" );
    
    while ( result.hasNext() ) {
        Record record = result.next();
        gson.toJson(record.asMap());
    }
    session.close();
    driver.close();