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
  [SOLVED] Loop through directories and files one level down? Winfried 3 268 Apr-28-2024, 02:31 PM
Last Post: Gribouillis
  encrypt data in json file help jacksfrustration 1 275 Mar-28-2024, 05:16 PM
Last Post: deanhystad
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 1,075 Feb-29-2024, 12:30 AM
Last Post: Winfried
  parse json field from csv file lebossejames 4 796 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Loop through values and compare edroche3rd 6 739 Oct-18-2023, 04:04 PM
Last Post: edroche3rd
  [SOLVED] [loop] Exclude ranges in… range? Winfried 2 1,536 May-14-2023, 04:29 PM
Last Post: Winfried
  Python Script to convert Json to CSV file chvsnarayana 8 2,599 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,513 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,695 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  validate large json file with millions of records in batches herobpv 3 1,313 Dec-10-2022, 10:36 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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