Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
json format
#1
Hi,

I need your help, please. I'm french. Sorry for my bad english !

I want to append a file with json format but I want to have line carrier return in the file to simplify read of the file.

I have a file with json records.
example :
ListeCharacters.json =
[{"surname" : "xxx", "forename" : "jean", "age" : 24}, {"surname" : "yyy", "forename" : "paul", "age" : 31}, {"surname" : "zzz", "forename" : "julia", "age" : 28}]

I want to append the file with a new record :
addjson = {"surname" : "aaa", "forename" : "Paul", "age" : 40}

The code after gives :
ListeCharacters.json =
[{"surname" : "xxx", "forename" : "jean", "age" : 24}, {"surname" : "yyy", "forename" : "paul", "age" : 31}, {"surname" : "zzz", "forename" : "julia", "age" : 28}, {"surname" : "aaa", "forename" : "Paul", "age" : 40}]

but I have several problems :
1/ I would like to append the file with "addjson" record. At the moment, my code delete file and create a new file. How to append file with a new information ?

2/ I would like to format my json file in another form :
ListeCharacters.json =
[
{"surname" : "xxx", "forename" : "jean", "age" : 24},
{"surname" : "yyy", "forename" : "paul", "age" : 31},
{"surname" : "zzz", "forename" : "julia", "age" : 28},
{"surname" : "aaa", "forename" : "Paul", "age" : 40}
]

It's to simplfy reading of file. Is it allowed with json format ? how to do this ?

Thanks a lot for your help. Smile
############################################################################\n",
fileName = "ListeCharacters.json"

try:
    with open(fileName) as json_file:
        liste = json.load(json_file)
        liste.append(addjson)
except IOError:
    liste = [addjson]
   
with open(fileName, "w") as outfile:
    json.dump(liste, outfile)
Reply


Messages In This Thread
json format - by Bubu93200 - Apr-21-2022, 03:15 PM
RE: json format - by deanhystad - Apr-21-2022, 03:34 PM
RE: json format - by Bubu93200 - Apr-21-2022, 04:43 PM
RE: json format - by davediamond - Apr-21-2022, 03:51 PM
RE: json format - by deanhystad - Apr-21-2022, 05:08 PM
RE: json format - by Bubu93200 - Apr-21-2022, 05:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert Json to table format python_student 4 15,377 Dec-05-2024, 04:32 PM
Last Post: Larz60+
  format json outputs ! evilcode1 3 2,785 Oct-29-2023, 01:30 PM
Last Post: omemoe277
  Converting cells in excel to JSON format desmondtay 4 3,047 May-23-2022, 10:31 AM
Last Post: Larz60+
  Return in correct json format UtiliseIT 3 3,836 May-13-2019, 11:24 AM
Last Post: snippsat
  how do I format json data in splunk? fxtyom 14 17,286 May-24-2017, 09:23 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

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