Python Forum
Append JSON's and write to file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Append JSON's and write to file
#1
I have a Sample JSON record in a file as below
{ "name":"John", "age":30, "car":"BMW" }

I want to create more JSON records out of it and i want to minimize the i/o by not writing to the file system everytime i create sample record, the code below is to change the age using parameters and hold the JSON records in a list and write to the file system when i am done generating the records.


    # Read the sample file
    with open("C:/myfile.json") as file:
        originalJSON = json.load(file)
    # How many sample Records?
    numberOfRecords = 4
    i = 1
    myOutput = [] # initializing a dummy list to append my json's
    while (i <= numberOfRecords):
            # Change the age
            originalJSON["age"] = i
            myOutput.append(originalJSON)
            i += 1
    # After appending all the JSON's to the list i want to write the JSON array to a file
    with open("C:/multiplerecords.json", 'w') as multipleJsons:
        json.dump(myOutput, multipleJsons)
The output looks like

[{ "name":"John", "age":4, "car":"BMW" }
{ "name":"John", "age":4, "car":"BMW" }
{ "name":"John", "age":4, "car":"BMW" }
{ "name":"John", "age":4, "car":"BMW" }]

$$$ the issue i see is, when i write myOutput list it generates 4 records in it but all of them are duplicates, the last json that was produced is written to the whole list. Instead i am looking for something like below

[{ "name":"John", "age":1, "car":"BMW" }
{ "name":"John", "age":2, "car":"BMW" }
{ "name":"John", "age":3, "car":"BMW" }
{ "name":"John", "age":4, "car":"BMW" }]


###################################################################################################################
Reply


Messages In This Thread
Append JSON's and write to file - by faqsap - May-13-2020, 09:27 PM
RE: Append JSON's and write to file - by bowlofred - May-14-2020, 06:34 AM
RE: Append JSON's and write to file - by faqsap - May-14-2020, 06:22 PM
RE: Append JSON's and write to file - by bowlofred - May-15-2020, 07:26 AM
RE: Append JSON's and write to file - by faqsap - May-15-2020, 04:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 12 28,099 Feb-13-2025, 04:48 AM
Last Post: tomhansky
  How to write variable in a python file then import it in another python file? tatahuft 4 944 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  Write json data to csv Olive 6 1,341 Oct-22-2024, 06:59 AM
Last Post: Olive
  [SOLVED] [Linux] Write file and change owner? Winfried 6 1,544 Oct-17-2024, 01:15 AM
Last Post: Winfried
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 1,079 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  What does .flush do? How can I change this to write to the file? Pedroski55 3 1,351 Apr-22-2024, 01:15 PM
Last Post: snippsat
  encrypt data in json file help jacksfrustration 1 2,232 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  Last record in file doesn't write to newline gonksoup 3 1,592 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  parse json field from csv file lebossejames 4 2,026 Nov-14-2023, 11:34 PM
Last Post: snippsat
  write to csv file problem jacksfrustration 11 5,157 Nov-09-2023, 01:56 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