Using Cypher to return nested, hierarchical JSON from a tree

后端 未结 2 1726
故里飘歌
故里飘歌 2020-12-14 11:31

I\'m currently using the example data on console.neo4j.org to write a query that outputs hierarchical JSON.

The example data is created with

create         


        
2条回答
  •  情深已故
    2020-12-14 12:31

    In neo4j 3.x, after you install the APOC plugin on the neo4j server, you can call the apoc.convert.toTree procedure to generate similar results.

    For example:

    MATCH p=(n:Crew {name:'Neo'})-[:KNOWS*]->(m)
    WITH COLLECT(p) AS ps
    CALL apoc.convert.toTree(ps) yield value
    RETURN value;
    

    ... would return a result row that looks like this:

        {
          "_id": 127,
          "_type": "Crew",
          "name": "Neo",
          "knows": [
            {
              "_id": 128,
              "_type": "Crew",
              "name": "Morpheus",
              "knows": [
                {
                  "_id": 129,
                  "_type": "Crew",
                  "name": "Trinity"
                },
                {
                  "_id": 130,
                  "_type": "Crew:Matrix",
                  "name": "Cypher",
                  "knows": [
                    {
                      "_id": 131,
                      "_type": "Matrix",
                      "name": "Agent Smith"
                    }
                  ]
                }
              ]
            }
          ]
        }
    

提交回复
热议问题