1

I have some JSON data that gives me information back about my "nodes". One of the data elements for a node is "children" which will contain zero through N number of numbers. What I am trying to accomplish is to start at the top level ID of the hierarchy and work my way down. I've tried create a dict with the name of the top-level element, then have lists within the dict, but I can't seem to iterate all the data without having a disgusting amount of nested if statements.

What I am trying to get out of it is a dict of lists eg: mydict = {'name':'mydatacenter', 'rooms':'a list of the child rooms (of the specific datacenter)', 'rows':'a list of the child row (of rooms) etc etc

Here is example JSON:

{
   "status": "OK",
   "output":
   {
       "nodes":
       [
           {
               "children":
               [
                   -6
               ],
               "type_id": 5,
               "type": "datacenter",
               "id": -7,
               "name": "mydatacenter"
           },
           {
               "children":
               [
                   -5
               ],
               "type_id": 4,
               "type": "room",
               "id": -6,
               "name": "myroom"
           },
           {
               "children":
               [
                   -4
               ],
               "type_id": 3,
               "type": "row",
               "id": -5,
               "name": "myrow"
           },
           {
               "children":
               [
                   -3,
                   -2
               ],
               "type_id": 2,
               "type": "rack",
               "id": -4,
               "name": "myrack"
           },
           {
               "children":
               [
                   1,
                   0
               ],
               "type_id": 1,
               "type": "host",
               "id": -2,
               "name": "myhost1"
           },
           {
               "status": "up",
               "name": "process.0",
               "exists": 1,
               "type_id": 0,
               "reweight": "1.000000",
               "crush_weight": "0.009995",
               "depth": 5,
               "type": "process",
               "id": 0
           },
           {
               "status": "up",
               "name": "process.1",
               "exists": 1,
               "type_id": 0,
               "reweight": "1.000000",
               "crush_weight": "0.009995",
               "depth": 5,
               "type": "process",
               "id": 1
           },
       "stray":
       [
       ]
   }
}

I found this example, which helps me understand a little bit, but I'm not sure how to handle the fact that I will always have to iterate from datacenter all the way down.

2
  • Can you give the code where you tried creating the dict with the top-level element? Having someplace to start would be useful. Commented Feb 8, 2014 at 5:48
  • I believe I did: for node in nodes: if node['type_id'] == 5: mydict[ node['name'] ] == node['name'] Commented Feb 8, 2014 at 6:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.