Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
07-03-2020 02:10 PM
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()
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
07-04-2020 05:22 AM
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.
07-06-2020 12:52 AM
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).
07-11-2020 07:29 PM
@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();
}
}
07-12-2020 05:31 AM
Thanks the feedback and posting your solution!
All the sessions of the conference are now available online