Python Forum

Full Version: Loop through json file and reset values [SOLVED]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everybody,

I have a json file which stores information for different people, which looks like this:
{
    "person": 
        [
            {
                {"id": "1",
                  "name": "Geroge"
                  "status": "unsent"
                 },
                {"id": "2",
                  "name": "Tim"
                  "status": "unsent"
                 }
        ]
}
Everytime my script get's executed I want to select a random person. Once the person has been selected it should change the value "status" to "sent"

def random_operation():
    with open('config.json', "r") as fp:
        data = json.load(fp)
        person = data["person"]
        random_index = randint(0, len(person)-1)
        index = (operation[random_index]['name']

        print(index)

    with open('config.json', "w") as fx:
        data['person'][int(random_index)]['status'] = "sent"
        json.dump(data, fx)
This works fine. Now I want that everytime the script gets executed it only selects people who have status = unsent. When it randomly selects a person whose status is unsent it should do something and if the script randomly selects a person whose status is sent it should repeat until it finds a person with status = unsent. Once every status has been changed to sent (and no one has status= unsent) all statuses should get reset to unsnet. I'm not sure how to do this. I've tried with While True but that trapped me in an infinite-loop...
Your json file is wrong.
Output:
{ "person": [ { <-- This does not belong {"id": "1", "name": "Geroge" "status": "unsent" }, {"id": "2", "name": "Tim" "status": "unsent" } ] }
The thing to remember is you are never using json. You are using Python dictionaries and lists. json is only used to read/write information to a file. It is no relevant.

Load the corrected json file. This produces a dictionary that looks like this:
people = {"person":[{"id":1, "name": "George", "status": "unsent"}, {"id":2, "name": "Tim", "status": "unsent"}]}

Create a list of all person objects that are not sent. I would do this with a comprehension.
unsent = [person for person in people["person"] if person.status == "unsent"]

Randomly select a person from the unsent list and change status to "sent"

dump people to a json file.
(Mar-23-2023, 08:34 PM)deanhystad Wrote: [ -> ]Your json file is wrong.
Output:
{ "person": [ { <-- This does not belong {"id": "1", "name": "Geroge" "status": "unsent" }, {"id": "2", "name": "Tim" "status": "unsent" } ] }
The thing to remember is you are never using json. You are using Python dictionaries and lists. json is only used to read/write information to a file. It is no relevant.

Load the corrected json file. This produces a dictionary that looks like this:
people = {"person":[{"id":1, "name": "George", "status": "unsent"}, {"id":2, "name": "Tim", "status": "unsent"}]}

Create a list of all person objects that are not sent. I would do this with a comprehension.
unsent = [person for person in people["person"] if person.status == "unsent"]

Randomly select a person from the unsent list and change status to "sent"

dump people to a json file.

I finally managed go get it running. Thanks for your tip with the comprehension Smile