JSON object parsing : multiple rows with multiple nodes in a REST API neo4j cypher response

北城余情 提交于 2019-12-25 16:39:54

问题


When playing around with JSON Neo4j response over REST API, i am trying to break my head to understand how to should i access a value returned, when my result is having one column and 2 rows . Each row in turn contains multiple nodes.

the query (gives me a travel plan) :

MATCH (a:LandMark {name:'Acharya College' }),(d:AirPort {name:'Frankfurt International Airport'})
MATCH p1 = allShortestPaths((a)-[:STOPS_AT*]-(d))
WITH p1, FILTER(j IN NODES(p1) WHERE(j in nodes(p1)) ) AS buses
RETURN buses

the response i understand is a nested array one, but i am unable to figure out which are the indexes to access the values.

the alert(JSON.stringify(data, null, 4)); gives:

 {    "columns": [        "buses"    ],    "data": [        [            [                {                    "labels": "http://localhost:7474/db/data/node/23/labels",                    "outgoing_relationships": "http://localhost:7474/db/data/node/23/relationships/out",                    "data": {                        "name": "Acharya College"                    },                    "all_typed_relationships": "http://localhost:7474/db/data/node/23/relationships/all/{-list|&|types}",                    "traverse": "http://localhost:7474/db/data/node/23/traverse/{returnType}",                    "self": "http://localhost:7474/db/data/node/23",                    "property": "http://localhost:7474/db/data/node/23/properties/{key}",                    "outgoing_typed_relationships": "http://localhost:7474/db/data/node/23/relationships/out/{-list|&|types}",                    "properties": "http://localhost:7474/db/data/node/23/properties",                    "incoming_relationships": "http://localhost:7474/db/data/node/23/relationships/in",                    "extensions": {},                    "create_relationship": "http://localhost:7474/db/data/node/23/relationships",                    "paged_traverse": "http://localhost:7474/db/data/node/23/paged/traverse/{returnType}{?pageSize,leaseTime}",                    "all_relationships": "http://localhost:7474/db/data/node/23/relationships/all",                    "incoming_typed_relationships": "http://localhost:7474/db/data/node/23/relationships/in/{-list|&|types}",                    "metadata": {                        "id": 23,                        "labels": [                            "LandMark"                        ]                    }                },                {                    "labels": "http://localhost:7474/db/data/node/25/labels",                    "outgoing_relationships": "http://localhost:7474/db/data/node/25/relationships/out",                    "data": {                        "name": "Auto Rickshaw"                    },                    "all_typed_relationships": "http://localhost:7474/db/data/node/25/relationships/all/{-list|&|types}",                    "traverse": "http://localhost:7474/db/data/node/25/traverse/{returnType}",                    "self": "http://localhost:7474/db/data/node/25",                    "property": "http://localhost:7474/db/data/node/25/properties/{key}",                    "outgoing_typed_relationships": "http://localhost:7474/db/data/node/25/relationships/out/{-list|&|types}",                    "properties": "http://localhost:7474/db/data/node/25/properties",                    "incoming_relationships": "http://localhost:7474/db/data/node/25/relationships/in",                    "extensions": {},                    "create_relationship": "http://localhost:7474/db/data/node/25/relationships",                    "paged_traverse": "http://localhost:7474/db/data/node/25/paged/traverse/{returnType}{?pageSize,leaseTime}",                    "all_relationships": "http://localhost:7474/db/data/node/25/relationships/all",                    "incoming_typed_relationships": "http://localhost:7474/db/data/node/25/relationships/in/{-list|&|types}",                    "metadata": {                        "id": 25,                        "labels": [                            "AutoRickShaw"                        ]                    }                },                {                    "labels": "http://localhost:7474/db/data/node/0/labels",                    "outgoing_relationships": "http://localhost:7474/db/data/node/0/relationships/out",                    "data": {                        "name": "CST International Airport:Mumbai"                    },                    "all_typed_relationships": "http://localhost:7474/db/data/node/0/relationships/all/{-list|&|types}",                    "traverse": "http://localhost:7474/db/data/node/0/traverse/{returnType}",                    "self": "http://localhost:7474/db/data/node/0",                    "property": "http://localhost:7474/db/data/node/0/properties/{key}",                    "outgoing_typed_relationships": "http://localhost:7474/db/data/node/0/relationships/out/{-list|&|types}",                    "properties": "http://localhost:7474/db/data/node/0/properties",                    "incoming_relationships": "http://localhost:7474/db/data/node/0/relationships/in",                    "extensions": {},                    "create_relationship": "http://localhost:7474/db/data/node/0/relationships",                    "paged_traverse": "http://localhost:7474/db/data/node/0/paged/traverse/{returnType}{?pageSize,leaseTime}",                    "all_relationships": "http://localhost:7474/db/data/node/0/relationships/all",                    "incoming_typed_relationships": "http://localhost:7474/db/data/node/0/relationships/in/{-list|&|types}",                    "metadata": {                        "id": 0,                        "labels": [                            "AirPort"                        ]                    }                },                {                    "labels": "http://localhost:7474/db/data/node/3/labels",                    "outgoing_relationships": "http://localhost:7474/db/data/node/3/relationships/out",                    "data": {                        "FlightNo": "LF1131"                    },                    "all_typed_relationships": "http://localhost:7474/db/data/node/3/relationships/all/{-list|&|types}",                    "traverse": "http://localhost:7474/db/data/node/3/traverse/{returnType}",                    "self": "http://localhost:7474/db/data/node/3",                    "property": "http://localhost:7474/db/data/node/3/properties/{key}",                    "outgoing_typed_relationships": "http://localhost:7474/db/data/node/3/relationships/out/{-list|&|types}",                    "properties": "http://localhost:7474/db/data/node/3/properties",                    "incoming_relationships": "http://localhost:7474/db/data/node/3/relationships/in",                    "extensions": {},                    "create_relationship": "http://localhost:7474/db/data/node/3/relationships",                    "paged_traverse": "http://localhost:7474/db/data/node/3/paged/traverse/{returnType}{?pageSize,leaseTime}",                    "all_relationships": "http://localhost:7474/db/data/node/3/relationships/all",                    "incoming_typed_relationships": "http://localhost:7474/db/data/node/3/relationships/in/{-list|&|types}",                    "metadata": {                        "id": 3,                        "labels": [                            "AirPlane"                        ]                    }                },                {                    "labels": "http://localhost:7474/db/data/node/2/labels",                    "outgoing_relationships": "http://localhost:7474/db/data/node/2/relationships/out",                    "data": {                        "name": "Frankfurt International Airport"                    },                    "all_typed_relationships": "http://localhost:7474/db/data/node/2/relationships/all/{-list|&|types}",                    "traverse": "http://localhost:7474/db/data/node/2/traverse/{returnType}",                    "self": "http://localhost:7474/db/data/node/2",                    "property": "http://localhost:7474/db/data/node/2/properties/{key}",                    "outgoing_typed_relationships": "http://localhost:7474/db/data/node/2/relationships/out/{-list|&|types}",                    "properties": "http://localhost:7474/db/data/node/2/properties",                    "incoming_relationships": "http://localhost:7474/db/data/node/2/relationships/in",                    "extensions": {},                    "create_relationship": "http://localhost:7474/db/data/node/2/relationships",                    "paged_traverse": "http://localhost:7474/db/data/node/2/paged/traverse/{returnType}{?pageSize,leaseTime}",                    "all_relationships": "http://localhost:7474/db/data/node/2/relationships/all",                    "incoming_typed_relationships": "http://localhost:7474/db/data/node/2/relationships/in/{-list|&|types}",                    "metadata": {                        "id": 2,                        "labels": [                            "AirPort"                        ]                    }                }            ]        ],        [            [                {                    "labels": "http://localhost:7474/db/data/node/23/labels",                    "outgoing_relationships": "http://localhost:7474/db/data/node/23/relationships/out",                    "data": {                        "name": "Acharya College"                    },                    "all_typed_relationships": "http://localhost:7474/db/data/node/23/relationships/all/{-list|&|types}",                    "traverse": "http://localhost:7474/db/data/node/23/traverse/{returnType}",                    "self": "http://localhost:7474/db/data/node/23",                    "property": "http://localhost:7474/db/data/node/23/properties/{key}",                    "outgoing_typed_relationships": "http://localhost:7474/db/data/node/23/relationships/out/{-list|&|types}",                    "properties": "http://localhost:7474/db/data/node/23/properties",                    "incoming_relationships": "http://localhost:7474/db/data/node/23/relationships/in",                    "extensions": {},                    "create_relationship": "http://localhost:7474/db/data/node/23/relationships",                    "paged_traverse": "http://localhost:7474/db/data/node/23/paged/traverse/{returnType}{?pageSize,leaseTime}",                    "all_relationships": "http://localhost:7474/db/data/node/23/relationships/all",                    "incoming_typed_relationships": "http://localhost:7474/db/data/node/23/relationships/in/{-list|&|types}",                    "metadata": {                        "id": 23,                        "labels": [                            "LandMark"                        ]                    }                },                {                    "labels": "http://localhost:7474/db/data/node/25/labels",                    "outgoing_relationships": "http://localhost:7474/db/data/node/25/relationships/out",                    "data": {                        "name": "Auto Rickshaw"                    },                    "all_typed_relationships": "http://localhost:7474/db/data/node/25/relationships/all/{-list|&|types}",                    "traverse": "http://localhost:7474/db/data/node/25/traverse/{returnType}",                    "self": "http://localhost:7474/db/data/node/25",                    "property": "http://localhost:7474/db/data/node/25/properties/{key}",                    "outgoing_typed_relationships": "http://localhost:7474/db/data/node/25/relationships/out/{-list|&|types}",                    "properties": "http://localhost:7474/db/data/node/25/properties",                    "incoming_relationships": "http://localhost:7474/db/data/node/25/relationships/in",                    "extensions": {},                    "create_relationship": "http://localhost:7474/db/data/node/25/relationships",                    "paged_traverse": "http://localhost:7474/db/data/node/25/paged/traverse/{returnType}{?pageSize,leaseTime}",                    "all_relationships": "http://localhost:7474/db/data/node/25/relationships/all",                    "incoming_typed_relationships": "http://localhost:7474/db/data/node/25/relationships/in/{-list|&|types}",                    "metadata": {                        "id": 25,                        "labels": [                            "AutoRickShaw"                        ]                    }                },                {                    "labels": "http://localhost:7474/db/data/node/0/labels",                    "outgoing_relationships": "http://localhost:7474/db/data/node/0/relationships/out",                    "data": {                        "name": "CST International Airport:Mumbai"                    },                    "all_typed_relationships": "http://localhost:7474/db/data/node/0/relationships/all/{-list|&|types}",                    "traverse": "http://localhost:7474/db/data/node/0/traverse/{returnType}",                    "self": "http://localhost:7474/db/data/node/0",                    "property": "http://localhost:7474/db/data/node/0/properties/{key}",                    "outgoing_typed_relationships": "http://localhost:7474/db/data/node/0/relationships/out/{-list|&|types}",                    "properties": "http://localhost:7474/db/data/node/0/properties",                    "incoming_relationships": "http://localhost:7474/db/data/node/0/relationships/in",                    "extensions": {},                    "create_relationship": "http://localhost:7474/db/data/node/0/relationships",                    "paged_traverse": "http://localhost:7474/db/data/node/0/paged/traverse/{returnType}{?pageSize,leaseTime}",                    "all_relationships": "http://localhost:7474/db/data/node/0/relationships/all",                    "incoming_typed_relationships": "http://localhost:7474/db/data/node/0/relationships/in/{-list|&|types}",                    "metadata": {                        "id": 0,                        "labels": [                            "AirPort"                        ]                    }                },                {                    "labels": "http://localhost:7474/db/data/node/3/labels",                    "outgoing_relationships": "http://localhost:7474/db/data/node/3/relationships/out",                    "data": {                        "FlightNo": "LF1131"                    },                    "all_typed_relationships": "http://localhost:7474/db/data/node/3/relationships/all/{-list|&|types}",                    "traverse": "http://localhost:7474/db/data/node/3/traverse/{returnType}",                    "self": "http://localhost:7474/db/data/node/3",                    "property": "http://localhost:7474/db/data/node/3/properties/{key}",                    "outgoing_typed_relationships": "http://localhost:7474/db/data/node/3/relationships/out/{-list|&|types}",                    "properties": "http://localhost:7474/db/data/node/3/properties",                    "incoming_relationships": "http://localhost:7474/db/data/node/3/relationships/in",                    "extensions": {},                    "create_relationship": "http://localhost:7474/db/data/node/3/relationships",                    "paged_traverse": "http://localhost:7474/db/data/node/3/paged/traverse/{returnType}{?pageSize,leaseTime}",                    "all_relationships": "http://localhost:7474/db/data/node/3/relationships/all",                    "incoming_typed_relationships": "http://localhost:7474/db/data/node/3/relationships/in/{-list|&|types}",                    "metadata": {                        "id": 3,                        "labels": [                            "AirPlane"                        ]                    }                },                {                    "labels": "http://localhost:7474/db/data/node/2/labels",                    "outgoing_relationships": "http://localhost:7474/db/data/node/2/relationships/out",                    "data": {                        "name": "Frankfurt International Airport"                    },                    "all_typed_relationships": "http://localhost:7474/db/data/node/2/relationships/all/{-list|&|types}",                    "traverse": "http://localhost:7474/db/data/node/2/traverse/{returnType}",                    "self": "http://localhost:7474/db/data/node/2",                    "property": "http://localhost:7474/db/data/node/2/properties/{key}",                    "outgoing_typed_relationships": "http://localhost:7474/db/data/node/2/relationships/out/{-list|&|types}",                    "properties": "http://localhost:7474/db/data/node/2/properties",                    "incoming_relationships": "http://localhost:7474/db/data/node/2/relationships/in",                    "extensions": {},                    "create_relationship": "http://localhost:7474/db/data/node/2/relationships",                    "paged_traverse": "http://localhost:7474/db/data/node/2/paged/traverse/{returnType}{?pageSize,leaseTime}",                    "all_relationships": "http://localhost:7474/db/data/node/2/relationships/all",                    "incoming_typed_relationships": "http://localhost:7474/db/data/node/2/relationships/in/{-list|&|types}",                    "metadata": {                        "id": 2,                        "labels": [                            "AirPort"                        ]                    }                }            ]        ]    ]}

回答1:


How do you access it?

Check out this javascript snippet on http://neo4j.com/developer/javascript/#_using_the_http_endpoint_directly

var r=require("request");
var txUrl = "http://localhost:7474/db/data/transaction/commit";
function cypher(query,params,cb) {
  r.post({uri:txUrl,
          json:{statements:[{statement:query,parameters:params}]}},
         function(err,res) { cb(err,res.body)})
}

// Running the Function

var query="MATCH (n:User) RETURN n, labels(n) as l LIMIT {limit}"
var params={limit: 10}
var cb=function(err,data) { console.log(JSON.stringify(data)) }

cypher(query,params,cb)

{"results":[
  {"columns":["n","l"],
   "data":[
     {"row":[{"name":"Aran"},["User"]]}
    ]
  }],
 "errors":[]}



回答2:


I know you're using php and that your question is related to this one : Parsing a neo4j JSON object in javascript

Why don't you take advantage of the existing drivers that will remove you the burden of the response parsing.

Here is an example using neoclient :

composer require neoxygen/neoclient

--

$client = ClientBuilder::create()
    ->addDefaultLocalConnection()
    ->build();

// Sending your query :
$query = "MATCH (a:LandMark {name:'Acharya College' }),(d:AirPort {name:'Frankfurt International Airport'})
MATCH p1 = allShortestPaths((a)-[:STOPS_AT*]-(d))
WITH p1, FILTER(j IN NODES(p1) WHERE(j in nodes(p1)) ) AS buses
RETURN buses";

$result = $client->sendCypherQuery($query)->getResult();

// Getting the buses

$buses = $result->get('buses'); // Just specify the return alias desired
// Returns you a collection of buses

// Passing what you want to your frontend :

$response = [];
foreach ($buses as $bus) {
    $response[] = [
        'bus_name' => $bus->getProperty('name'),
        'bus_id' => $bus->getProperty('id')
        ];

return json_encode($response);

And in your javascript you only need to iterate over the response ;-)



来源:https://stackoverflow.com/questions/28276803/json-object-parsing-multiple-rows-with-multiple-nodes-in-a-rest-api-neo4j-cyph

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!