cancel
Showing results for 
Search instead for 
Did you mean: 

How to create relationships for multiple nodes? (need help)

Charles
Node

Hi everyone! I'm very new at Neo4j.
I'm trying to create a graph from a json file that contains the links for the graph and the nodes.
The "links" section has the number of the source node called "ori_val", and the node's number target for this link called "target".
In the "nodes" section lies the "id" label, wich is the number of the corresponding node for creating it, and this "id" number also is the one used in the "links" section for "ori_val" and "target".

Here is an example of the json file for the first node and link (there are 1138 nodes, and 3553 links):

{     
      "links":  {"0":
                        "target"	:	376,
                         "value"	:	3,
                        "source"	:	0,
                       "ori_val"	:	3,
                          "type"	:	"normal"},
      "nodes": {"0": 
                          "country": "Guatemala",
                          "case_id": 0,
                             "year": "2019",
                               "id": 0,
                             "name": "Caso Villaseñor Velarde y otros Vs. Guatemala",
                             "type": 1 }
}
                 

I tried to built the graph by running this code on the cypher, but it only makes links from all the nodes to just two of them, and all the remainig links between the other nodes are missed.
I've also created the nodes from the file with the label "NODOS".

call apoc.load.json("graph.json") yield value
unwind value.links as l
MATCH (n:NODOS) WHERE n.id=l.ori_val 
MATCH (s:NODOS) WHERE s.id=l.target
CREATE (n)-[:Incide]->(s)

Sorry if this is an obvious question, but I've spent too many days trying to solve it and I can´t.

Here's an example of how the graph is displayed for the first 25 nodes, and all the links between the remaining nodes are missed (don't pay attention to the "R" relationship).
Thanks for yout help!
3X_0_5_058627002bc8c8e048525a0cb9bb0a21f66f28cb.png

6 REPLIES 6

clem
Graph Steward

Your JSON looks malformed to me [Note: this comment was based on an older version of the question]:

The 0 should be a string and there should be a value after it. Or maybe it should be removed. Or something. Check your log to see if apoc.load.json() complained about your JSON.

Also, the string values after the keys need to be inside quotes. (e.g. "normal" "Guatemala")

Your JSON flunked this checker: https://jsonlint.com/

{
"links": {0:
"target" : 376,
"value" : 3,
"source" : 0,
"ori_val" : 3,
"type" : normal},
"nodes": {0:
"country": Guatemala,
"case_id": 0,
"year": 2019,
"id": 0,
"name": Caso Villaseñor Velarde y otros Vs. Guatemala
"type": 1 }
}

Hi, thanks for your help!
Indeed it is wrong, was a mistake of mine while posting it on here. But the original one has the correct syntaxis.

You can edit your original post.

I'm not clear from your post what's exactly your problem is. Can you explain further? Sorry...

Sure, no problem.
I'm trying to build a graph with this json.
First you have the "links" section, this tells you all the links your graph must have, in this example you need to build the link from the node with "id"=3, to the node with "id"=376, and so on with the others 3552 links in this json.
On the other hand, you have the "nodes" section, wich tells you all the nodes you have to create, and its properties like "id", "country", etc. I have already created all the nodes from the "nodes" section, with the label "NODOS".
My problem is when I try to build all the links for all the created nodes. I did it with this code:

call apoc.load.json("graph.json") yield value
unwind value.links as l
MATCH (n:NODOS) WHERE n.id=l.ori_val 
MATCH (s:NODOS) WHERE s.id=l.target
CREATE (n)-[:Incide]->(s)

But this only makes the links from all the nodes to just two of them: the once with "Corte Interamericana" label, and the other one labeled with "Convencion Americana sobre Derechos". And the other relationships between the remaining nodes are missing, for example the link between "Caso Godínez" and "Caso Herrera" is missed, and so on.
I need to know what am I missing to make all the relationships between the other nodes?

It's still broken. the key

"0" :

isn't followed by a proper value

From JSON lint

Error: Parse error on line 3:
...": {		"0": "target": 376,		"value": 3,

Did you look at the logs? I'm not sure how easy it is for apoc functions to show errors in the browser.

Sorry again, here is the good one.

{     
      "links":  {"0": {
                        "target"	:	376,
                         "value"	:	3,
                        "source"	:	0,
                       "ori_val"	:	3,
                          "type"	:	"normal"}},
      "nodes": {"0": {
                          "country": "Guatemala",
                          "case_id": 0,
                             "year": "2019",
                               "id": 0,
                             "name": "Caso Villaseñor Velarde y otros Vs. Guatemala",
                             "type": 1 }}
}