Search code examples
jsonangularjsrestneo4jneo4jrestclient

Accessing JSON data from Neo4j REST response in Angularjs


I'm a newbie with rest and angular, so my hope answer to my question is super easy.

I'm having problem working with JSON response I get from new Neo4j post transaction/commit query.

I want to access response data for each item I have in the response. I've searched how others handle this, but have found no same cases. I think I do not parse the response at all, and can not access the specific row.

Here is my code, that just prints all the json.

JS controller

function restcall($scope, $http) {

  var call = '{ "statements" : [ { "statement" : "MATCH (n:Cars) RETURN n ORDER BY n.initRank DESC LIMIT 10" } ] }';

  $http({
                method: 'POST',
                url: 'http://myserver:7474/db/data/transaction/commit',
                data: call,

 })
 .success(function (data, status) {
                        $scope.status = status;
                        $scope.response = data.results;
            })
 .error(function (data, status) {
                        $scope.response = data || "Request failed";
                        $scope.status = status;
            })

        };

HTML that just prints out complete response

<section ng-controller="restcall">
        <h2>{{status}}</h2>
        </br></br>
        <h3>{{response}}</h3>
</section>

And most importantly the JSON response I get

{

"results":[{

    "columns":[
        "n"
    ],

    "data":[
        {"row":[{"name":"Car1","initRank":"..."}]},
        {"row":[{"name":"Car2","initRank":"..."}]},
        {"row":[{"name":"Car3","initRank":"..."}]},
        {"row":[{"name":"Car4","initRank":"..."}]},
        {"row":[{"name":"Car5","initRank":"..."}]},
        {"row":[{"name":"Car6","initRank":"..."}]}]
}],

"errors":[]

}

So basically now I just print out in html my json response.

Now, how do I access individual rows to get i.e. Car3 properties??

I tried the data.results[0][0].data... and also to parse my string, but when I add next .data it just doesn't show a thing, same thing with parsing.. Can someone help please.


Solution

  • Based on that JSON response, you would use data.results[0].data[2].row[0].initRank to access the "initRank" of Car3. You shouldn't need to do any extra parsing of the response. It should already be an object in your callback.