Roman March
Roman March

Reputation: 73

Parse neo4j JSON response

I have the neo4j response Object (I'm prividing you the whole response so I'd get it right):

result = {
    "records": [
        {
            "keys": [
                "criteria.name"
            ],
            "length": 1,
            "_fields": [
                "Perspective"
            ],
            "_fieldLookup": {
                "criteria.name": 0
            }
        },
        {
            "keys": [
                "criteria.name"
            ],
            "length": 1,
            "_fields": [
                "3D"
            ],
            "_fieldLookup": {
                "criteria.name": 0
            }
        },
        {
            "keys": [
                "criteria.name"
            ],
            "length": 1,
            "_fields": [
                "2D"
            ],
            "_fieldLookup": {
                "criteria.name": 0
            }
        }
    ],
    "summary": {
        "query": {
            "text": "MATCH (criteria:TEST_01)\nRETURN criteria.name\nLIMIT 3",
            "parameters": {}
        },
        "queryType": "r",
        "counters": {
            "_stats": {
                "nodesCreated": 0,
                "nodesDeleted": 0,
                "relationshipsCreated": 0,
                "relationshipsDeleted": 0,
                "propertiesSet": 0,
                "labelsAdded": 0,
                "labelsRemoved": 0,
                "indexesAdded": 0,
                "indexesRemoved": 0,
                "constraintsAdded": 0,
                "constraintsRemoved": 0
            },
            "_systemUpdates": 0
        },
        "updateStatistics": {
            "_stats": {
                "nodesCreated": 0,
                "nodesDeleted": 0,
                "relationshipsCreated": 0,
                "relationshipsDeleted": 0,
                "propertiesSet": 0,
                "labelsAdded": 0,
                "labelsRemoved": 0,
                "indexesAdded": 0,
                "indexesRemoved": 0,
                "constraintsAdded": 0,
                "constraintsRemoved": 0
            },
            "_systemUpdates": 0
        },
        "plan": false,
        "profile": false,
        "notifications": [],
        "server": {
            "address": "localhost:7687",
            "version": "Neo4j/4.1.0",
            "protocolVersion": 4.1
        },
        "resultConsumedAfter": {
            "low": 2,
            "high": 0
        },
        "resultAvailableAfter": {
            "low": 80,
            "high": 0
        },
        "database": {
            "name": "neo4j"
        }
    }
}

I need to get only individual "_fields" from it as following:

{"Perspective", "3D", "2D"}

How do I do this?

I ended up suceeded getting the value from any exact record with this code:

a = Object.values(result.records)
b = Object.values(a[0]._fields)
console.log(b);

but I can't understand how can I go fore each of "a" elemets in array and extract "_fields".

Upvotes: 3

Views: 302

Answers (1)

Nisala
Nisala

Reputation: 1338

You can use a for ... of loop to go through all objects in records!

const result = {
    "records": [
        {
            "keys": [
                "criteria.name"
            ],
            "length": 1,
            "_fields": [
                "Perspective"
            ],
            "_fieldLookup": {
                "criteria.name": 0
            }
        },
        {
            "keys": [
                "criteria.name"
            ],
            "length": 1,
            "_fields": [
                "3D"
            ],
            "_fieldLookup": {
                "criteria.name": 0
            }
        },
        {
            "keys": [
                "criteria.name"
            ],
            "length": 1,
            "_fields": [
                "2D"
            ],
            "_fieldLookup": {
                "criteria.name": 0
            }
        }
    ],
    "summary": {
        "query": {
            "text": "MATCH (criteria:TEST_01)\nRETURN criteria.name\nLIMIT 3",
            "parameters": {}
        },
        "queryType": "r",
        "counters": {
            "_stats": {
                "nodesCreated": 0,
                "nodesDeleted": 0,
                "relationshipsCreated": 0,
                "relationshipsDeleted": 0,
                "propertiesSet": 0,
                "labelsAdded": 0,
                "labelsRemoved": 0,
                "indexesAdded": 0,
                "indexesRemoved": 0,
                "constraintsAdded": 0,
                "constraintsRemoved": 0
            },
            "_systemUpdates": 0
        },
        "updateStatistics": {
            "_stats": {
                "nodesCreated": 0,
                "nodesDeleted": 0,
                "relationshipsCreated": 0,
                "relationshipsDeleted": 0,
                "propertiesSet": 0,
                "labelsAdded": 0,
                "labelsRemoved": 0,
                "indexesAdded": 0,
                "indexesRemoved": 0,
                "constraintsAdded": 0,
                "constraintsRemoved": 0
            },
            "_systemUpdates": 0
        },
        "plan": false,
        "profile": false,
        "notifications": [],
        "server": {
            "address": "localhost:7687",
            "version": "Neo4j/4.1.0",
            "protocolVersion": 4.1
        },
        "resultConsumedAfter": {
            "low": 2,
            "high": 0
        },
        "resultAvailableAfter": {
            "low": 80,
            "high": 0
        },
        "database": {
            "name": "neo4j"
        }
    }
}

let option1 = [];
let option2 = [];

// For each object in result.records,
for (let val of result.records) {
  // Put everything in val._fields into our result array.
  // ... spreads the array, so all elements are inserted
  // individually in case, in the future,
  // there are multiple items in _fields.
  option1.push(...val._fields);
  
  // For the object you provided, you could just do
  // val._fields[0]. However, option1 is more generalizable
  // in case there's ever more than one thing in _fields.
  option2.push(val._fields[0]);
}

console.log(option1);
console.log(option2);

Upvotes: 2

Related Questions