How to extract JSON results (JS driver)

based on this question

How do I get at a JSON result from the query?

I have a cypher query that formats the results. When logging with (let row of result.records) {
I get something like the below. There is JSON there but it's under _fields[0] which looks like a private variable I shouldn't just parse.

Is there a way to get something like result.value ?

row 0 Record {
  keys: [
    '{\n' +
      '        graph: {\n' +
      '          nodes: [n in nodes|n{.name}],\n' +
      '          links: [r in rels| {\n' +
      '            name: r.name,\n' +
      '            source: startNode(r).name,\n' +
      '            target: endNode(r).name\n' +
      '          }]\n' +
      '        }\n' +
      '      }'
  ],
  length: 1,
  _fields: [ { graph: [Object] } ],
  _fieldLookup: {
    '{\n        graph: {\n          nodes: [n in nodes|n{.name}],\n          links: [r in rels| {\n            name: r.name,\n            source: startNode(r).name,\n            target: endNode(r).name\n          }]\n        }\n      }': 0
  }
}

FWIW my cypher looks something like this:

    const query = `
      // get all nodes and links for a chapter
      MATCH
        (ch:CHAPTER{uid:$chapterId})-[r1]-> (t1)-[r2]->(t2)
      with
        COLLECT(DISTINCT t1) as nodes,
        collect(DISTINCT r2) as rels
      return {
        graph: {
          nodes: [n in nodes|n{.name}],
          links: [r in rels| {
            name: r.name,
            source: startNode(r).name,
            target: endNode(r).name
          }]
        }
      }`

    const res = await neoConn().runTx(query, {
      chapterId: this.data.id!,
    })

update: copilot gives this which seems to work:

const val = res.records[0].get(0)

Yes, because you only have one record in your results since you aggregated everything, so record[0] works. You are returning only one column 'graph', so getting the zeroth item from the record[0] works.

1 Like