Skip to content

Instantly share code, notes, and snippets.

@sujinleeme
Last active May 21, 2019 12:20
Show Gist options
  • Save sujinleeme/9034d380b3c0fb02c566dabc832c0857 to your computer and use it in GitHub Desktop.
Save sujinleeme/9034d380b3c0fb02c566dabc832c0857 to your computer and use it in GitHub Desktop.
Convert a JSON's file tree structure

Convert a JSON's file tree structure

If you want to make the tree command available on mac OS X, run brew install tree.

Simply, change BASE_DIR and OUTPUT_JSON_FILENAME. Run convert-file-tree-json.py.

You can use the tree command which lists a directory structure recursively.

e.g)

.
├── Adam\ Drive
│   ├── 103.815038-1.3362878.jpg
│   └── 103.815246-1.336219.jpg
├── Adam\ Park
│   ├── 103.813987-1.328999.jpg
│   └── 103.814129-1.329334.jpg
├── Adam\ Road
│   ├── 103.8134801-1.3241393.jpg
│   └── 103.813994-1.3284652.jpg
└── Adis\ Road
    ├── 103.847286-1.300006.jpg
    └── 103.847336-1.2999752.jpg

The output *.json might be :

{
  "name": "images-new",
  "type": "directory",
  "children": [
    {
      "name": "Adam Park",
      "type": "directory",
      "children": [
        {
          "name": "103.813987-1.328999.jpg",
          "type": "image"
        },
        {
          "name": "103.814129-1.329334.jpg",
          "type": "image"
        }
      ]
    },
    {
      "name": "Adam Road",
      "type": "directory",
      "children": [
        {
          "name": "103.813994-1.3284652.jpg",
          "type": "image"
        },
        {
          "name": "103.8134801-1.3241393.jpg",
          "type": "image"
        }
      ]
    },
    {
      "name": "Adam Drive",
      "type": "directory",
      "children": [
        {
          "name": "103.815038-1.3362878.jpg",
          "type": "image"
        },
        {
          "name": "103.815246-1.336219.jpg",
          "type": "image"
        },
      ]
    },
    {
      "name": "Adis Road",
      "type": "directory",
      "children": [
        {
          "name": "103.847286-1.300006.jpg",
          "type": "image"
        },
        {
          "name": "103.847336-1.2999752.jpg",
          "type": "image"
        }
      ]
    }
  ]
}
import os
import json
def check_in_file(my_file,my_string):
with open(my_file) as f:
try:
return my_string in f.read()
except:
return False
def path_to_dict(path, my_string=None):
d = {'name': os.path.basename(path)}
if os.path.isdir(path):
d['type'] = "directory"
d['children'] = []
paths = [os.path.join(path,x) for x in os.listdir(path)]
#Just the children that contains at least a valid file
for p in paths:
c = path_to_dict(p, my_string)
if c is not None:
d['children'].append(c)
if not d['children']:
return None
else:
if my_string is not None and not check_in_file(path,my_string):
return None
d['type'] = "image"
return d
BASE_DIR = 'images'
OUTPUT_JSON_FILENAME = 'image_file.json'
with open(f'{OUTPUT_JSON_FILENAME}', 'w') as f:
content = json.dumps(path_to_dict(f'{BASE_DIR}',), indent=2)
f.write(content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment