Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Represent hierarchy in a JSON object

I need to represent this hierarchy in a JSON object. Can someone help me out ?

- John
--- Lee
------ Nash
--------- Tim
------ Nicole
------ Kelly
--- Alice
--- Stanley
like image 630
YD8877 Avatar asked Dec 09 '25 01:12

YD8877


2 Answers

{
  "name": "John", 
  "children": [ 
    {
      "name": "Lee", 
      "children": [
         {
           "name": "Nash", 
           "children": [{ "name":"Tim"}]
         },
         {
           "name": "Nicole"
         },
         {
           "name": "Kelly"
         }
      ]
    },
    {
      "name": "Alice"
    },
    {
      "name": "Stanley" 
    } 
  ] 
}
like image 172
DrStrangeLove Avatar answered Dec 10 '25 13:12

DrStrangeLove


How about this:

{
    "John" : {
        "Lee" : {
            "Nash" : {
                "Tim" : null 
            },
            "Nicole" : null,
            "Kelly" : null 
        },
        "Alice" : null,
        "Stanley" : null 
    }
}

The relationship, whether it be children or otherwise, is implied by the tree hierarchy.

like image 44
surj Avatar answered Dec 10 '25 14:12

surj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!