Jlearninpython Jlearninpython - 1 year ago 92
Python Question

Parse a Directory of json files with python

I have been looking for how to do this but I cannot.
I have a directory of .json files and I am supposed to parse each one.
I know I have to use glob and os.
I feel like the logic behind it is loop over the directory and when reading each file extract the data that is needed, but I cannot find anywhere to help me nor do I know the syntax.
If its against stack rules and people think this is me asking for answers to homework that is fine I can just search elsewhere, this isn't homework I just don't understand.

Answer Source

Assuming that your JSON files are named with a .json extension and that they are in the same directory that you are running the script from:

import json
from glob import glob

data = []
for file_name in glob('*.json'):
    with open(file_name) as f:

This will give you a list of parsed JSON objects (dictionaries and/or lists).

If you need to access the files in another directory you can construct your glob pattern like this:

import os.path

pattern = os.path.join('/path/to/json/files', '*.json')
for file_name in glob(pattern):
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download