Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to find information in json file what i need and print those file names?

I have a silly question.
So basically, there is allot of JSON files and, for example, i need find to only the JSON files that contains only that information i need now and print those file names that contains that information what i need?

So there’s an example:
i have an directory where the files are at
and this is the information what JSON files contain

{
    "version": "1.0.4",
    "room": {
        "index": 2,
        "label": "Bedroom",
        "score": 0.999368429,
        "threshold": 0.6,
        "raw": {
            "Balcony": {
                "index": 0,
                "score": 0.000324130058
            },
            "Bathroom": {
                "index": 1,
                "score": 8.77380371e-05
            },
            "Bedroom": {
                "index": 2,
                "score": 0.999368429
            },
            "Dining Room": {
                "index": 3,
                "score": 0.000696450472
            },
            "Garage": {
                "index": 4,
                "score": 0.000141739845
            },
            "Hallway": {
                "index": 5,
                "score": 3.37660313e-05
            },
            "Kitchen": {
                "index": 6,
                "score": 4.11570072e-05
            },
            "Laundry Room": {
                "index": 7,
                "score": 9.4473362e-06
            }
,...       
        }
    }
}

Now i need to find all JSON files that contain "label": "Bedroom" and print those file names.
Its just for me. I’m learning. Thanks in advance.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can loop through the files in the current directory and check if it contains the information you need.

import os

# The directory that you want to search in. Let's assume that it is named "data"
DIR = "data"
data = "" # The data to check against. I'm not going to write that here, as it is too long.

for file in os.listdir(DIR):
    with open(file, "r") as f:
        if f.readlines() == data:
            print(file)
            break

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading