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.

Create a graph from json

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

I have json file 

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 us-east-1, value of longitude and value of latitude and the edges will have the latency and the cost

 

1 ACCEPTED SOLUTION

Try this; it seems to work.  I didn't evaluate any of the other apoc json procedures; I just took the first one. It may be easier with another one. Basically, it loops through each location to create the nodes, then it makes a second pass to create the links, now that they exist. 

call apoc.load.json("file:///location.json")
yield value
with value.locations_connections.locations as locationInfo
forEach(i in locationInfo |
    merge(n:Location{id: i.id}) 
    on create 
    set n.latitude = i.latitude, 
    n.longitude = i.longitude
)
with locationInfo
unwind locationInfo as location
match(n:Location{id: location.id}) 
unwind location.connections as connection
call{
    with n, connection
    match(m:Location{id: connection.to})
    merge(n)-[r:CONNECTED_TO]->(m)
    on create
    set r.latency = connection.latency,
    r.cost = connection.cost
}

Screen Shot 2022-07-07 at 9.00.36 AM.png

View solution in original post

2 REPLIES 2

Try this; it seems to work.  I didn't evaluate any of the other apoc json procedures; I just took the first one. It may be easier with another one. Basically, it loops through each location to create the nodes, then it makes a second pass to create the links, now that they exist. 

call apoc.load.json("file:///location.json")
yield value
with value.locations_connections.locations as locationInfo
forEach(i in locationInfo |
    merge(n:Location{id: i.id}) 
    on create 
    set n.latitude = i.latitude, 
    n.longitude = i.longitude
)
with locationInfo
unwind locationInfo as location
match(n:Location{id: location.id}) 
unwind location.connections as connection
call{
    with n, connection
    match(m:Location{id: connection.to})
    merge(n)-[r:CONNECTED_TO]->(m)
    on create
    set r.latency = connection.latency,
    r.cost = connection.cost
}

Screen Shot 2022-07-07 at 9.00.36 AM.png

thank you very much