-1

Using python, how can I take sample set of json data below, and organize it into an array or list of some sort? The reason I want to do this is so I can then more easily loop through this variable recursively and create a treeview with parent/child relationships.

I'm mainly looking for help on how I would create a nested dictionary out of my json data.

enter image description here

Pseudo Goal

'Studio Tools'
    'Modeling'
        'C:/Users/jmartini/Desktop/Trash/submitters/run.ms'
        'C:/Users/jmartini/Projects/tinichest_simple/someTools\\CreateSphere.py'
        'Sculpting'
            'C:/Users/jmartini/Projects/tinichest_simple/someTools\\Exporter.py'
    'Lighting'
        'C:/Users/jmartini/Projects/tinichest_simple/someTools/Mover.ms'
        'C:/Users/jmartini/Projects/tinichest_simple/someTools\\DemoTest.py'

JSON EXAMPLE

files =[
    {
        'category':[
            'Studio Tools',
            'Modeling'
        ],
        'file':'C:/Users/jmartini/Desktop/Trash/submitters/run.ms'
    },
    {
        'category':[
            'Studio Tools',
            'Lighting'
        ],
        'file':'C:/Users/jmartini/Projects/tinichest_simple/someTools/Mover.ms'
    },
    {
        'category':[
            'Studio Tools',
            'Modeling'
        ],
        'file':'C:/Users/jmartini/Projects/tinichest_simple/someTools\\CreateSphere.py'
    },
    {
        'category':[
            'Studio Tools',
            'Lighting'
        ],
        'file':'C:/Users/jmartini/Projects/tinichest_simple/someTools\\DemoTest.py'
    },
    {
        'category':[
            'Studio Tools',
            'Modeling',
            'Sculpting'
        ],
        'file':'C:/Users/jmartini/Projects/tinichest_simple/someTools\\Exporter.py'
    }
]
JokerMartini
  • 5,674
  • 9
  • 83
  • 193

1 Answers1

0

There is the json module in python:

import json
with open("input.json") as json_file:
    data=json.load(json_file)

for key, value in data.items():
    print("json key: ", key)
    print("json value: ", str(value))

The json module will import the data into a python dict.

Actually this dict is already a tree-like datastructure. It doesn't offer the classical pointer to parents/children. But I think you will find the python for loops extremely comfortable, they are great for processing such data.

lhk
  • 27,458
  • 30
  • 122
  • 201
  • I know how to use the json module, im more looking for help on how i would condense the json data into a nested dictionary – JokerMartini Nov 03 '16 at 12:12
  • The json module will import the data as a nested dictionary – lhk Nov 03 '16 at 12:13
  • The category keys in your sample will point to lists after the import. If there is a key which maps to another JSON object. Something like "key": {...}, this key will map to a list. – lhk Nov 03 '16 at 12:14
  • so how would that create the output I'm looking for then? – JokerMartini Nov 03 '16 at 12:17
  • The markup doesn't correspond to the final format. "Studio Tools" is an entry in the array, just as "Modeling" or "Lighting". You need "expert knowledge" about the expected output in order to parse the input. You can of course search in the loaded data for something like "studio tools" and if you find it, assume that it is in a list and look at the other items in the list. This is possible but will produce ugly code. A better approach would be to clean up the markup in the first place. It's in your own interest to define a proper markup format, you will profit in the long run – lhk Nov 03 '16 at 13:05
  • How would you suggest I do a better markup? I'm open to ideas. – JokerMartini Nov 03 '16 at 13:07