HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Creating a directory structure from JSON

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
directorycreatingstructurejsonfrom

Problem

I have a JSON file:

{
'Document':[

[{'fields': {'name': 'js/main.js', 'content': 'hello'}, 'pk': 284, 'model': 'Document'}],

[{'fields': {'name': 'css/main.css', 'content': '2'}, 'pk': 287, 'model': 'Document'}],

[{'fields': {'name': 'about_us.html', 'content': 'again hello'}, 'pk': 306, 'model': 'Document'}]],

'Package':
[{'fields': {'package_type': 'THEME', 'base_package': None, 'created_date': '2015-05-25T15:39:16.781Z', 'name': '25_may', 'rating_avg': 0.0, 'user': 2, 'rating_count': 0, 'is_published': True}, 'pk': 129, 'model': 'Package'}]

}


I want to generate directory structure based on JSON data:

25_may
|- css
|  |- main.css
|- js
|  |-main.js    
|- about_us.html


Code to achieve above:

def my_func():
    package_name = response['Package'][0]['fields']['name']
    for doc in response['Document']:                
        filename = os.path.join(package_name, doc[0]['fields']['name'])
        content = doc[0]['fields']['content']
        if not os.path.exists(os.path.dirname(filename)):
            os.makedirs(os.path.dirname(filename))
        with open(filename, "w") as f:
            f.write(content)


Is there an efficient and better way to do this?

Solution

It seems fine, but I suggest to improve some minor improvements:

  • filename is not a simple file name but has directory element too, so I'd call it path



  • Instead of calling os.path.dirname twice, I'd call it once and cache the result in a local variable



Like this:

package_name = response['Package'][0]['fields']['name']
for doc in response['Document']:                
    path = os.path.join(package_name, doc[0]['fields']['name'])
    basedir = os.path.dirname(filename)
    if not os.path.exists(basedir):
        os.makedirs(basedir)

    content = doc[0]['fields']['content']
    with open(filename, "w") as f:
        f.write(content)


Btw, the JSON structure looks a bit odd,
with several arrays with a single element.
Makes me wonder why these arrays are there at all,
instead of their contained objects directly.

I'd also be concerned about format changes in the JSON:
your code is tightly coupled to an unintuitive structure,
and if anything changes later,
it might be difficult to migrate the implementation.

Code Snippets

package_name = response['Package'][0]['fields']['name']
for doc in response['Document']:                
    path = os.path.join(package_name, doc[0]['fields']['name'])
    basedir = os.path.dirname(filename)
    if not os.path.exists(basedir):
        os.makedirs(basedir)

    content = doc[0]['fields']['content']
    with open(filename, "w") as f:
        f.write(content)

Context

StackExchange Code Review Q#91829, answer score: 2

Revisions (0)

No revisions yet.