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]
#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


Messages In This Thread
RE: Loop through json file and reset values - by AlphaInc - Apr-06-2023, 11:15 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Loop through directories and files one level down? Winfried 3 329 Apr-28-2024, 02:31 PM
Last Post: Gribouillis
  encrypt data in json file help jacksfrustration 1 321 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,197 Feb-29-2024, 12:30 AM
Last Post: Winfried
  parse json field from csv file lebossejames 4 816 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Loop through values and compare edroche3rd 6 752 Oct-18-2023, 04:04 PM
Last Post: edroche3rd
  [SOLVED] [loop] Exclude ranges in… range? Winfried 2 1,578 May-14-2023, 04:29 PM
Last Post: Winfried
  Python Script to convert Json to CSV file chvsnarayana 8 2,639 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,597 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,855 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  validate large json file with millions of records in batches herobpv 3 1,335 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