cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Creating graph from JSON using neo4J query

I'm new in neo4j and i have this json file:

{
"locations_connections": {
"locations": [
{
"id": "aws.us-west-1",
"longitude": 103.8517837,
"latitude": 1.287950,
"connections": [
{
"to": "aws.us-west-1",
"latency": 3.16,
"cost": 0.02
},
{
"to": "aws.us-east-1",
"latency": 53.47,
"cost": 0.02
},
{
"to": "aws.us-east-2",
"latency": 53.47,
"cost": 0.02
}
]
},
{
"id": "aws.us-east-2",
"longitude": 126.9780,
"latitude": 37.5665,
"connections": [
{
"to": "aws.us-east-1",
"latency": 3.16,
"cost": 0.02
},
{
"to": "aws.us-east-2",
"latency": 1.47,
"cost": 0.02
}
]
},
{
"id": "aws.us-east-1",
"longitude": 103.8517837,
"latitude": 1.287950,
"connections": [
{
"to": "aws.us-east-1",
"latency": 3.16,
"cost": 0.02
}
]
}
]
}
}

After reading the json using the apoc.load.json(URL) procedure , what query do I write to represent this as a graph?

where the Node will contain the information name like for example aws.us-east-1, value of longitude and value of latitude and the edges will have the latency and the cost

I have this one:

call apoc.load.json("/file.json") yield value
UNWIND value.locations_connections.locations as loc
UNWIND loc.connections as con
MERGE (e:Element {id:loc.id}) ON CREATE
SET e.longitude = loc.longitude, e.latitude = loc.latitude
MERGE (e1:Element {id: con.to, latency:con.latency, cost:con.cost})
MERGE (e)-[:CONNECTED]->(e1)
and it works but it's not exactly what i need,  could you tell me please what the request should be?

 

1 REPLY 1

Not sure what your data model looks like that you would want to do, just looking at your json file I would not have included the latency, cost in the target element (e1) but just the con.to 

call apoc.load.json("/file.json") yield value
UNWIND value.locations_connections.locations as loc
UNWIND loc.connections as con
MERGE (e:Element {id:loc.id}) ON CREATE
SET e.longitude = loc.longitude, e.latitude = loc.latitude
// remove the other elements from the merge-key
MERGE (e1:Element {id: con.to})
// CREATE instead of MERGE to allow for multiple connections, 
// if you don't have a unique connection-id
CREATE (e)-[c:CONNECTED]->(e1)
SET c.latency=con.latency, c.cost=con.cost