3
\$\begingroup\$

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?

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked May 26, 2015 at 13:49
\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

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.

answered May 26, 2015 at 17:49
\$\endgroup\$
1
  • \$\begingroup\$ Thanks for your suggestion for json structure. I will change it's format. \$\endgroup\$ Commented May 26, 2015 at 18:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.