Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8125289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:46:03+00:00 2026-06-06T06:46:03+00:00

I have folder structure and I would like to create JSON objects based on

  • 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T06:46:05+00:00Added an answer on June 6, 2026 at 6:46 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My folder structure is like - root admin create_page.php pages my_page1.php my_page2.php I have
I have a folder structure that I would like to have in my bundle.
I have a folder structure like this: /some_folder /tmp /tmp/foo /tmp/foo/fu * /tmp/bar /tmp/bar/bah
I have a folder structure like this: /articles .index.php .second.php .third.php .fourth.php If I'm
If I have a folder structure that looks like this: / /bin/myComponent.cfc /reports/index.cfm How
I have the following folder structure /project /scrape item.py /spiders myscraper.py inside the file
I have a folder structure at server side. Inside the folders there are files
I have some images on my server folder and I would like to know
I would like to have a the following structure of folders: Myproj\src Myproj\include Myproj\lib
I would like to know What are the difference between folder-structure and package used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.