Python Forum
Loop through json file and reset values [SOLVED]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop through json file and reset values [SOLVED]
#1
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...
Reply
#2
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.
Reply
#3
Thumbs Up 
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Access keys and values from nested dictionary? Winfried 4 913 Nov-17-2025, 11:47 AM
Last Post: Winfried
Question [SOLVED] Linefeed when writing "f" strings to text file? Winfried 5 857 Nov-04-2025, 11:51 AM
Last Post: buran
  Errors using json file RonR 5 955 Oct-28-2025, 03:00 PM
Last Post: buran
Question [SOLVED] Open file, and insert space in string? Winfried 7 2,523 May-28-2025, 07:56 AM
Last Post: Winfried
Question [SOLVED] [Beautiful Soup] Replace tag.string from another file? Winfried 2 1,672 May-01-2025, 03:43 PM
Last Post: Winfried
  [SOLVED] [Linux] Write file and change owner? Winfried 6 3,192 Oct-17-2024, 01:15 AM
Last Post: Winfried
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 2,250 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  [solved] how to delete the 10 first lines of an ascii file paul18fr 7 4,177 Aug-07-2024, 08:18 PM
Last Post: Gribouillis
  [SOLVED] Loop through directories and files one level down? Winfried 3 4,379 Apr-28-2024, 02:31 PM
Last Post: Gribouillis
  encrypt data in json file help jacksfrustration 1 3,568 Mar-28-2024, 05:16 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020