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?