cancel
Showing results for 
Search instead for 
Did you mean: 

Neo4J CYPHER in C# or Java: Return JSON output from “call db.schema.nodeTypeProperties()”?

james_mireles
Node Link

When calling db.schema.nodeTypeProperties() from within the Neo4J Broswer, the Code side tab returns the complete JSON schema in the Response drop-down. Is it possible to retrieve this JSON result in C# or Java using the Neo4J.Driver? I would like to deserialize the JSON text into C# classes.

Screen-cap of Response drop-down from db.schema.nodeTypeProperties()
2X_f_ff3c75f50f1905202b516e86ac54b871ce8ab459.png

I have explored the Neo4J.Driver IDriver , IAsyncSession and IResultCursor calls and cannot find a way to retrieve the JSON dataset.

Any suggestions?

James Mireles
Houston, TX

4 REPLIES 4

webtic
Graph Fellow

with a query like this:

call db.schema.nodeTypeProperties() yield nodeType,nodeLabels,propertyName,propertyTypes,mandatory 
return collect( {type:nodeType,labels:nodeLabels,name:propertyName,type:propertyTypes,m:mandatory})```

you can transform the data to an object in the shape you want and return it to the driver as JSON.

Another option you have is to use the REST endpoint, something like:

var client = new RestClient("http://localhost:7474");
client.Authenticator = new HttpBasicAuthenticator("neo4j", "neo");

var query = "{\"statements\" : [ { \"statement\":\"call db.schema.nodeTypeProperties()\", \"parameters\" : {} }] }";

var request = new RestRequest("db/neo4j/tx/commit",DataFormat.Json);
request.AddJsonBody(query);

var content = client.Post(request).Content;

That should be in JSON (I'm using RestSharp in the code).

james_mireles
Node Link

@webtic, @charlotte.skardon, thank you both for your suggestions. I experimented with them and other approaches and was able to get what I wanted by using apoc.export.json.all to stream in a JSON dataset of the entire database. I leveraged the examples at https://neo4j.com/docs/labs/apoc/current/export/json/#export-database-json. I apologize for what turned out to be the misdirection of my question. db.schema.nodeTypeProperties() was not going to get me what I really needed.

using Newtonsoft.Json.Linq;

public async void TestNeo4j()
{
    // Set up the graph database driver and connect the session to the Neo4J database.
    IDriver driver = GraphDatabase.Driver(Neo4JBoltURI, AuthTokens.Basic(Neo4JUser, Neo4JPassword));
    IAsyncSession session = driver.AsyncSession();
    IResultCursor cursor;

    try
    {
        // Bring the JSON text in as a stream
        string query = "CALL apoc.export.json.all(null,{stream:true,useTypes:true}) " +
            "YIELD file, nodes, relationships, properties, data " +
            "RETURN file, nodes, relationships, properties, data";

        cursor = await session.RunAsync(query);
        string sJsonData = await cursor.SingleAsync(record => record["data"].As<string>());
        //Debug.Log(sJsonData);

        //// Save the JSON to a file.
        //string path = @"C:\Users\Public\Documents\Neo4JExportAll.json";
        //if (File.Exists(path)) File.Delete(path);
        //File.WriteAllText(path, sJsonData);

        // Each line is a separate JSON statement describing a node or a relationship
        // Iterate all statements
        using (StringReader reader = new StringReader(sJsonData))
        {
            string line = string.Empty;
            do
            {
                line = reader.ReadLine();
                if (line != null)
                {
                    // Deserialize the JSON line into JObject jo.
                    JObject jo = JObject.Parse(line);
                    // Dig into the JObject to get the data from the stream.
                }

            } while (line != null);
        }
    }
    finally
    {
        await session.CloseAsync();
    }
}

Thanks the feedback and posting your solution!

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.