pythonneo4jbolt

Neo4j results to JSON in Python


While python Neo4j library, with bolt driver, when executing queries by session.run(query) I receive a result object that can be manipulated perfectly. In the other hand Neo4j browser interface, when executing a query, in table tab, it returns a string formatted as a JSON.

Is there any function in neo4j python library that converts the results python object to a JSON string to the table in web interface?

Using .data() method over the records returns me a simple json instead a more complete one from web interface.

For example from Python .data() I receive the following dictionary (that can be later converted to json)

{'n': {'name': 'dog'}}

While the same result in web interface returns the following string

{
  "identity": 19,
  "labels": [
    "NOUN"
  ],
  "properties": {
    "name": "dog"
  },
  "elementId": "4:a94a1b6d-fb11-4844-a6e9-31362e907dd0:19"
}

As we can see the .data() from python loses a lot of information as labels and IDs.


Solution

  • Assuming you are using Cypher queries, you can change your query to explicitly return the desired fields for each node or relationship.

    Here is a snippet for outputting the desired fields for 2 nodes per result record:

    // Get (n) and (m) nodes
    .
    .
    .
    // 
    RETURN
      {identity: ID(n), labels: LABELS(n), properties: PROPERTIES(n), elementId: ELEMENTID(n)} AS n,
      {identity: ID(m), labels: LABELS(m), properties: PROPERTIES(m), elementId: ELEMENTID(m)} AS m