Help creating graph from json file

Hi,
I am trying to create a graph from some json files, but I am stack in the beggining. I load the file with apoc, ok, but then...how I create nodes and relationships from them? For example, in the following json file, how could I extract the info from the "lenght" file?

{
"3uon": {
"sequence": "MDD",
"length": 467,
"dataType": "LIGAND BINDING SITES"
}
}

I am working on a desktop 1.4.1, database 4.2.1 edition enterprise, apoc 4.2.0.
I hope somebody can help me. Also, if somebody could point me to some advanced tutorial/online course of cipher that would be even better. Somewhere where it covers how to extract data from the dictionaries/lists of json.
Thanks!!

Hi @cirauquipharma may I suggest to look at this article?

(disclamer I'm the writer)

In it I explain how to use the apoc.graph.fromDocument procedure that automatically transforms JSONs in graphs.

1 Like

What I have found helpful (at least with CSV files), is to take it step by step.

First, see if you can create a Node. And as a first step to that, see if you can create a table of the properties of the nodes (not yet created).

CALL apoc.some_call("file:///somefile") YIELD value 
RETURN value.field1, value.field2, ...
LIMIT 10

Then you should get a table. It's going to be a bit more complex than what I've shown because it's JSON, and you have to chase all the JSON elements.

If that works, then create the node:

CALL apoc.some_call("file:///somefile") YIELD value 
CREATE (n:Label{ field1:value.field1, field2:value.field2, ...})

don't forget you might want to convert strings into, e.g.

length:toInt(value.length)

After that works, then you have to figure out the relationships, which is not clear in your data example.

Sometimes, to make it conceptually simple, I will create the nodes in a first pass.

Then, in a second pass, I'll MATCH the existing nodes and then do a creation of the relationships. Something like:

CALL apoc.some_call("file:///somefile") YIELD value 
MATCH(n1:LabelA, {property1:value.field1}
MATCH(n2:LabelB, {property2:value.field2}
MERGE(n1)-[:RELATIONSHIP]->(n2) # or CREATE
RETURN n1,n2

Once you get the hang of it, then it may be possible to do it one pass.

However, I think what I've outlined in sketchy detail will give you a method of learning about your data and Neo4J in bite-sized steps.

I hope that helps.

Thanks! That is a very good tip actually. Step by step, I am managing to extract the info from json. The UNWIND command is being very useful to extract the data inside each json list and dictionaries.
The problem in my code above is the '3uon', json does not accept it because of number 3. I am still wondering how to do it. Changing it to 'code', I managed with something like:
CALL apoc.load.json("file:////Users/nuria/Dropbox/Projetos/CRIMS_FBDD/DesignGraphDB/Testes/teste4.json")
YIELD value
UNWIND value.code AS lig
MERGE (l:Lenght {len:lig.length})
RETURN l;

What is 3uon the name of?

I believe it is possible to use back ticks to quote property names that are "funny". I don't think Label names can be escaped. See:

Hi!
Yes, in fact, finally I managed. It was not working with '3uon', it seems that in neo4j it has to be 3uoninstead :slight_smile:
This 3uon is a code. I retrieved the json from a wesite of protein structures. That is the code for that protein. But in the json file/neo4j , it was a variable, and you cannot use variables starting with numbers...well, I see now that you can, using the ``. Yupi! Just learning! :slight_smile: