0

I have folder structure and I would like to create JSON objects based on the folder names in the root folder and the files below in the file tree. The file names are well structured like this:

objectNNN.xyz

Where NNN is a number like 001, 002 ... and xyz could be .png, .jpg, .eps or .mp3

the folder structure is like this (input to the script):

fruits

  • images

    • apple001.jpg
    • apple002.jpg
    • apple003.jpg
    • orange001.jpg
    • orange002.png
    • orange003.jpg
  • sounds

    • apple001.mp3
    • apple002.mp3
    • orange001.mp3

animals

  • ... etc

foods

  • ... etc

... based on this FOLDER structure, I'd like to read all the "sets" (fruits, animals, etc) and create a JSON object for each set like below: (note that the "word" key is take from all the object names in the images directory).

sets = {
animals: [ // this is from the folder name in the root folder
{
  word: "cat", // this is from the filename in the images directory eg cat001.jpg
  images: [
    {
      path: "images/basic/cat001.jpg"
    }, {
      path: "images/basic/cat002.jpg"
    }
  ],
  sounds: [ // based on all the images look for sounds
    {
      path: "sounds/basic/cat001.mp3"
    }, {
      path: "sounds/basic/cat002.mp3"
    }
  ]
}, // etc more sets and words
Junuxx
  • 14,011
  • 5
  • 41
  • 71
hagope
  • 5,523
  • 7
  • 38
  • 52

1 Answers1

2

The question is rather ill-defined, but here is my best shot at interpreting what you want. Extending this to adding file-paths and more groups of items is left as an exercise to the reader.

from re import split
from itertools import groupby, izip

fruits = set(["apple", "pear"])
animals = set(["bear", "cat"])

json_structure = {}

text_input = ["apple001.png", "apple002.jpg", "bear001.png", "pear001.png", "cat001.png"]

def check(filename):
"""Checks which group a filename is in and returns an integer for ordering"""
    n = split(r'[0-9]', filename)[0]
    if n in fruits:
        return 0
    else:
        return 1


grouped = groupby(sorted(text_input, key=check), key=check)

for objects, object_type in izip(grouped, ["fruits", "animals"]):
    items = objects[1]
    json_structure[object_type] = list(items)

print json_structure
Wes
  • 2,100
  • 1
  • 17
  • 31
  • This is helpful, although not quite what I was looking for... I've scoped this directly to Node.js and cleared up what I'm looking for here: http://stackoverflow.com/questions/11194287/convert-a-directory-structure-in-the-filesystem-to-json-with-node-js – hagope Jun 25 '12 at 17:35