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
#2
originalJSON is a python object.

On line 10 you modify the object. On line 11, you append the object to your list. But none of these operations makes a copy. All 4 items in the list are the same object.

Probably you want to make a copy of it at the time you append.
        myOutput.append(copy.copy(originalJSON))
Reply
#3
Thank you for the reply
is copy.copy a valid command? I tried it and python spit out an error.
Reply
#4
Generally if you see a command in two parts like that, it indicates that it's part of a library. It's a standard library in this case, but you'll still need to import it.

import copy
within your script.

https://docs.python.org/3/library/copy.html
Reply
#5
Thank you for that explanation, it really helped. I ended up using the deep copy function.
Appreciate your help.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with reading json file hhchenfx 5 285 Jul-07-2021, 01:58 PM
Last Post: hhchenfx
  Read and write active Excel file euras 4 363 Jun-29-2021, 11:16 PM
Last Post: Pedroski55
  Read file, reformat and write new file bryanmartin113 1 340 Jun-08-2021, 09:27 PM
Last Post: Larz60+
  .json overwrites, rather than append?? Clives 12 706 Jun-07-2021, 04:13 PM
Last Post: buran
  Request for Python code - Features extraction from JSON file (cuckoo) thinker 1 434 Apr-07-2021, 04:40 PM
Last Post: Larz60+
  write mariadb table rows query to each file? shams 1 573 Feb-02-2021, 04:10 PM
Last Post: buran
  Read/Write binary file deanhystad 3 733 Feb-01-2021, 10:29 AM
Last Post: Larz60+
  Running A Parser In VSCode - And Write The Results Into A Csv-File apollo 5 1,002 Jan-14-2021, 08:58 PM
Last Post: snippsat
  Sort data from JSON file Dummy_in_programming 2 538 Jan-04-2021, 06:17 PM
Last Post: deanhystad
  Json File more pages #pandas #dataframe nio74maz 0 482 Dec-30-2020, 05:32 AM
Last Post: nio74maz

Forum Jump:

User Panel Messages

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