1

I have data in JSON format:

data = {"outfit":{"shirt":"red,"pants":{"jeans":"blue","trousers":"khaki"}}}

I'm attempting to plot this data into a decision tree using InfoVis, because it looks pretty and interactive. The problem is that their graph takes JSON data in this format:

data = {id:"nodeOutfit",
    name:"outfit",
    data:{},
    children:[{
        id:"nodeShirt",
        name:"shirt",
        data:{},
        children:[{
            id:"nodeRed",
            name:"red",
                   data:{},
                   children:[]
        }],
     },  {
         id:"nodePants",
         name:"pants",
         data:{},
         children:[{
             id:"nodeJeans",
             name:"jeans",
             data:{},
             children:[{
                    id:"nodeBlue",
                    name:"blue",
                    data:{},
                    children[]
             },{
             id:"nodeTrousers",
             name:"trousers",
             data:{},
             children:[{
                    id:"nodeKhaki",
                    name:"khaki",
                    data:{},
                    children:[]
          }
    }

Note the addition of 'id', 'data' and 'children' to every key and value and calling every key and value 'name'. I feel like I have to write a recursive function to add these extra values. Is there an easy way to do this?

Here's what I want to do but I'm not sure if it's the right way. Loop through all the keys and values and replace them with the appropriate:

for name, list in data.iteritems():
    for dict in list:
        for key, value in dict.items():
            #Need something here which changes the value for each key and values
            #Not sure about the syntax to change "outfit" to name:"outfit" as well as
            #adding id:"nodeOutfit", data:{}, and 'children' before the value

Let me know if I'm way off.

Here is their example http://philogb.github.com/jit/static/v20/Jit/Examples/Spacetree/example1.html

And here's the data http://philogb.github.com/jit/static/v20/Jit/Examples/Spacetree/example1.code.html

3
  • What's wrong with a recursive solution? Commented Dec 5, 2012 at 16:20
  • My issue right now is changing "outfit" to name:"outfit". Don't know how to manipulate individual keys/values. I could still use the recursion to work my way through the structure. Commented Dec 5, 2012 at 16:22
  • As you iterate over the dict, store the key and recursively process the value. Commented Dec 5, 2012 at 16:44

1 Answer 1

1

A simple recursive solution:

data = {"outfit":{"shirt":"red","pants":{"jeans":"blue","trousers":"khaki"}}}
import json
from collections import OrderedDict

def node(name, children):
    n = OrderedDict()
    n['id'] = 'node' + name.capitalize()
    n['name'] = name
    n['data'] = {}
    n['children'] = children
    return n

def convert(d):
    if type(d) == dict:
        return [node(k, convert(v)) for k, v in d.items()]
    else:
        return [node(d, [])]

print(json.dumps(convert(data), indent=True))

note that convert returns a list, not a dict, as data could also have more then one key then just 'outfit'.

output:

[
 {
  "id": "nodeOutfit", 
  "name": "outfit", 
  "data": {}, 
  "children": [
   {
    "id": "nodeShirt", 
    "name": "shirt", 
    "data": {}, 
    "children": [
     {
      "id": "nodeRed", 
      "name": "red", 
      "data": {}, 
      "children": []
     }
    ]
   }, 
   {
    "id": "nodePants", 
    "name": "pants", 
    "data": {}, 
    "children": [
     {
      "id": "nodeJeans", 
      "name": "jeans", 
      "data": {}, 
      "children": [
       {
        "id": "nodeBlue", 
        "name": "blue", 
        "data": {}, 
        "children": []
       }
      ]
     }, 
     {
      "id": "nodeTrousers", 
      "name": "trousers", 
      "data": {}, 
      "children": [
       {
        "id": "nodeKhaki", 
        "name": "khaki", 
        "data": {}, 
        "children": []
       }
      ]
     }
    ]
   }
  ]
 }
]
Sign up to request clarification or add additional context in comments.

5 Comments

Perfect! Can't import OrederedDict though. Gotta find a way around it.
@ono - I just used OrderedDict so the order of the elements stays constant, but that shouldn't really matter, just use dict() instead.
Last question: How do I remove the initial brackets '['? It won't plot if they're there. I tried doing the first element (data[0]) but it reordered the data.
try json.dumps(convert(data)[0])
@ono don't forget to upvote mata's answer, esp since he responded to all your others questions as well

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.