Using Cypher to return nested, hierarchical JSON from a tree

后端 未结 2 1722
故里飘歌
故里飘歌 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:13

    This was such a useful thread on this important topic, I thought I'd add a few thoughts after digging into this a bit further.

    First off, using the APOC "toTree" proc has some limits, or better said, dependencies. It really matters how "tree-like" your architecture is. E.g., the LOVES relation is missing in the APOC call above and I understand why – that relationship is hard to include when using "toTree" – that simple addition is a bit like adding an attribute in a hierarchy, but as a relationship. Not bad to do but confounds the simple KNOWS tree. Point being, a good question to ask is “how do I handle such challenges”. This reply is about that.

    I do recommend upping ones JSON skills as this will give you much more granular control. Personally, I found my initial exploration somewhat painful. Might be because I'm an XML person :) but once you figure out all the [, {, and ('s, it is really a powerful way to efficiently pull what's best described as a report on your data. And given the JSON is something that can easily become a class, it allows for a nice way to push that back to your app.

    I have found perf to also be a challenge with "toTree" vs. just asking for the JSON. I've added below a very simplistic look into what your RETURN could look like. It follows the following BN format. I'd love to see this more maturely created as the possibilities are quite varied, but this was something I'd have found useful thus I’ll post this immature version for now. As they say; “a deeper dive is left up to the readers”

    0 讨论(0)
  • 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"
                    }
                  ]
                }
              ]
            }
          ]
        }
    
    0 讨论(0)
提交回复
热议问题