Parsing complex JSON results from Neo4j

Viewed 2469

First, Thanks in advance for reading this. Second, Sorry in advance for the long post - but I hope like a well-written function, the good stuff is at the top - although please take the time to read it all. Third, I've lost track of how many stack questions I've looked through - if this is still a noob question I apologize.

I'm using Node.js to provide api's for my AngularJS client. All my data aggregation and transformation will be done in Node and present a flat JSON data structure to the client.

I have a Neo4j model that relates me to the applications I'm responsible for and relates technical risks to all applications. I have a nice Cypher query that shows me the tech risks for only the applications that I'm responsible for and excludes all my other apps that have no risk. Here's a pic of my query results: Neo4j Graph result.

Here's my node.js code (file routes.js called by the main api.js file):

var router = require('express').Router();
var neo4j = require('neo4j');
var db = new neo4j.GraphDatabase('http://user:password@localhost:7474');

router.get('/techrisks', getTechRisks);

module.exports = router;

function getTechRisks(req, res) {

    db.cypher({
        query: 'MATCH p=(a)-[e:ARCHITECT_FOR]->(b)-[d:HAS_RISK]->(c) WHERE a.shortname="macdonb" RETURN nodes(p) AS n,relationships(p) AS m',
         params: {

        }
    }, function (err, results) {

        if (err) { throw err; }
        var result = results[0];
        if (!result) {
            console.log('No TechRisk found.');
        } else {
            console.log(results);
            console.log(results[0]);
            console.log(results[1]);
        }
    });
}

The code above produces the following JSON (with separators added to the chunks for readability) Produces these results:

- - - - console.log(results); - - - -

[ { n: [ [Object], [Object], [Object] ],
    m: [ [Object], [Object] ] },
  { n: [ [Object], [Object], [Object] ],
    m: [ [Object], [Object] ] },
  { n: [ [Object], [Object], [Object] ],
    m: [ [Object], [Object] ] },
  { n: [ [Object], [Object], [Object] ],
    m: [ [Object], [Object] ] },
  { n: [ [Object], [Object], [Object] ],
    m: [ [Object], [Object] ] } ]

- - - - console.log(results[0]); - - - -

{ n:
   [ Node { _id: 585, labels: [Object], properties: [Object] },
     Node { _id: 675, labels: [Object], properties: [Object] },
     Node { _id: 695, labels: [Object], properties: [Object] } ],
  m:
   [ Relationship {
       _id: 845,
       type: 'ARCHITECT_FOR',
       properties: [Object],
       _fromId: 585,
       _toId: 675 },
     Relationship {
       _id: 813,
       type: 'HAS_RISK',
       properties: [Object],
       _fromId: 675,
       _toId: 695 } ] }

- - - - console.log(results[1]); - - - -

{ n:
   [ Node { _id: 585, labels: [Object], properties: [Object] },
     Node { _id: 674, labels: [Object], properties: [Object] },
     Node { _id: 689, labels: [Object], properties: [Object] } ],
  m:
   [ Relationship {
       _id: 844,
       type: 'ARCHITECT_FOR',
       properties: [Object],
       _fromId: 585,
       _toId: 674 },
     Relationship {
       _id: 810,
       type: 'HAS_RISK',
       properties: [Object],
       _fromId: 674,
       _toId: 689 } ] }

The n: array is confounding me. I started ripping it apart manually (trying split and splice with strings and objects respectively) but I believe it must be accessible using a json parser. When I use the following as a test:

var tmpResult1 = JSON.stringify(result.n);

I get a great string representation of n:

[{"_id":585,"labels":["Person"],"properties":{"hrpno":"00061627","lastName":"MacDonald","title":"Consultant, IT Architecture","hrdno":"104134","shortname":"macdonb","role":"Systems Architect","displayName":"Bruce MacDonald","firstName":"Bruce"}},{"_id":650,"labels":["Application"],"properties":{"dateverified":"2016-01-19","name":"Portal - Loss Runs","aprmid":"aprm1249"}},{"_id":683,"labels":["TechRisk"],"properties":{"status":"Documented","riskid":"ABC-2012-082","dateEntered":"2012-06-29"}}]

The string is nice but when I try to reference the array with dots or brackets I get a lot of "undefined" errors.

I'm a little lost and about ready to take another day or two break from it. I've looked for tutorials on YouTube - "about 49,300 results" (!!!) and of the 30+ I've looked at they all deal with simple structures and use "Movies" as the example.

Again, thanks for reading this far! I appreciate any help or clues.

3 Answers
Related