Script to generate directory from json file

Introduction

Suppose you have a json file like this:

sample.json



{
    "data": [
        {
            "no": "1",
            "date": "2020-01-28T00:00",
            "place": "japan",
            "age": "22",
            "sex": "female"
        },
        {
            "no": "2",
            "date": "2020-02-14T00:00",
            "place": "australia",
            "age": "50",
            "sex": "male"
        }
    ],
    "last_update": "2020-03-14T23:14:01.849130+09:00"
}

A script that creates the following directories from this file.

スクリーンショット 2020-03-15 16.42.01.png

It means that the hierarchy is divided by the index of key and list.

json2dir https://github.com/Kanahiro/json2dir

Recursively parses the json file and returns a list of directories to generate.

sample.py


import json2dir

jsondict = {
#jsondict is the sample mentioned above.Read json as dict type
}

root_dir = 'api/sample'
os.makedirs(root_dir, exist_ok=True) #api/Create a directory called sample

#Generate a directory list with this script
dirs = json2dir.dir_list_of(jsondict, root_dir)
'''
dir = ['api/sample/data', 'api/sample/data/0', 'api/sample/data/0/no', 'api/sample/data/0/date', 
'api/sample/data/0/place', 'api/sample/data/0/age', 'api/sample/data/0/sex', 'api/sample/data/1', 
'api/sample/data/1/no', 'api/sample/data/1/date', 'api/sample/data/1/place', 'api/sample/data/1/age', 
'api/sample/data/1/sex', 'api/sample/last_update']
'''

#api/Create a directory based on dir under sample
for d in dirs:
    os.makedirs(d, exist_ok=True)

The directory is created.

At the end

By the way, a package called json-to-dir is distributed by pip, but (probably) it was written in Python 2.x series and did not work properly (so I made it). I'd like to make it a pip package if there seems to be a need, but I've met my needs so I'm putting it off for the time being.

Recommended Posts

Script to generate directory from json file
Python script to create a JSON file from a CSV file
How to generate a Python object from JSON
Generate URL query from JSON
Parse JSON file to object
I made a tool to generate Markdown from the exported Scrapbox JSON file
Execute Python script from batch file
[python] Copy script to generate copy log
Save the Pydrive authentication file in a different directory from the script
Script to organize LDOS and PDOS from VASP output file DOSCAR
[Note] Read a file from another directory
Execute a script from Jupyter to process
Pin current directory to script directory in Python
Script to create a Mac dictionary file
Script python file
Easy script migration from CentOS to Oracle Linux
Import Excel file from Python (register to DB)
From file to graph drawing in Python. Elementary elementary
How to create a JSON file in Python
Script to create FlatBuffers binaries from SQL database
Sum from 1 to 10
[AWS; Introduction to Lambda] 2nd; Extract sentences from json file and save S3 ♬
Recursively search the specified directory to see the file
How to read a file in a different directory
How to use NUITKA-Utilities hinted-compilation to easily create an executable file from a Python script
I want to see the file name from DataLoader
Parse a JSON string written to a file in Python
Template of python script to read the contents of the file
Preferences to generate animated GIFs from Python on Mac
Backtrader How to import an indicator from another file
A memorandum to run a python script in a bat file
How to convert JSON file to CSV file with Python Pandas
[Python] Change standard input from keyboard to text file
[Python3] I want to generate harassment names from Japanese!
How to change static directory from default in Flask
Procedure to exe python file from Ubunts environment construction
How to convert Json file to CSV format or EXCEL format
Convert Pascal VOC format xml file to COCO format json file
Generate a bash script to add Datadog monitor settings
Automatic conversion from MySQL Workbench mwb file to sql file
[Python] File / directory operations
I made a script in Python to convert a text file for JSON (for vscode user snippet)
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Transition from WSL1 to WSL2
How to read JSON
Convert json to excel
From editing to execution
Assigned scaffolding macro in Python script file to F12 key
Convert Select query obtained from Postgre with Go to JSON
Use Boto3 to retrieve over 1000 Prefixes from S3's file list
A Python script that saves a clipboard (GTK) image to a file.
How to run a Python program from within a shell script
[Linux] Copy data from Linux to Windows with a shell script
Access the file with a relative path from the execution script.
Linux script to convert Markdown files from JupyterLab format to Qiita format
How to generate a public key from an SSH private key
Procedure from HTML to JSON Ajax communication of API server
[For beginners] Script within 10 lines (4. Connection from python to sqlite3)
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
[Caffe] Convert mean file from binary proto format to npy format