0

I have an issue within Jupyter that I cannot find online anywhere and was hoping I could get some help.

Essentially, I want to open .JSON files from multiple folders with different names. For example.

data/weather/date=2022-11-20/data.JSON
data/weather/date=2022-11-21/data.JSON
data/weather/date=2022-11-22/data.JSON
data/weather/date=2022-11-23/data.JSON

I want to be able to output the info inside the data.JSON onto my Jupyter Notebook, but how do I do that as the folder names are all different.

Thank you in advance.

What I tried so far

for path,dirs,files in os.walk('data/weather'): for file in files: if fnmatch.fnmatch(file,'*.json'): data = os.path.join(path,file) print(data)

OUTPUT:

data/weather/date=2022-11-20/data.JSON
data/weather/date=2022-11-21/data.JSON
data/weather/date=2022-11-22/data.JSON
data/weather/date=2022-11-23/data.JSON

But i dont want it to output the directory, I want to actually open the .JSON and display its content

Mercury
  • 3,417
  • 1
  • 10
  • 35
data-help
  • 19
  • 2
  • Uh. Did you try googling "how to open json file in python"? – Mercury Nov 28 '22 at 03:45
  • @Mercury please be helpful, not unnecessarily rude – Tomward Matthias Nov 28 '22 at 04:04
  • @TomwardMatthias Hello, I apologize if that came off as rude! However, this question clearly goes against stackoverflow standards. The asker isn't facing any error or any problem; they simply haven't even tried searching for their problem online, or let alone on S/O. This is almost on the same level as asking, *how to round a number in python*. You can take a look at the [How To Ask](https://stackoverflow.com/help/how-to-ask) page, for reference. – Mercury Nov 28 '22 at 09:34
  • The appropriate course of action in this circumstance is to not post an answer, but to point the asker with a comment to do the basic lookup. Even more appropriate: to link to [an existing answer](https://stackoverflow.com/questions/20199126/reading-json-from-a-file) and to mark as duplicate. – Mercury Nov 28 '22 at 09:37
  • Does this answer your question? [Reading JSON from a file](https://stackoverflow.com/questions/20199126/reading-json-from-a-file) – Mercury Nov 28 '22 at 09:40

1 Answers1

1

This solution uses the os library to go thru different directories

import os
import json

for root, dirs, files in os.walk('data/weather'):
    for file in files:
        if file.endswith('.JSON'):
            with open(os.path.join(root, file), 'r') as f:
                data = json.load(f)
                print(data)
Tomward Matthias
  • 127
  • 1
  • 13