Posts: 8
Threads: 2
Joined: Apr 2022
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.
############################################################################\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)
Posts: 6,827
Threads: 20
Joined: Feb 2020
Load json, modify and dump is the correct method. You do not want to append the file.
You should not worry about the format of your json data. Json files are easy to read, but are not designed to be human readable. You can make the file a little prettier using the indent argument.
import json
data = [
{"surname" : "xxx", "forename" : "jean", "age" : 24},
{"surname" : "yyy", "forename" : "paul", "age" : 31},
{"surname" : "zzz", "forename" : "julia", "age" : 28},
{"surname" : "aaa", "forename" : "Paul", "age" : 40}
]
with open("test.json", "w") as outfile:
json.dump(data, outfile, indent=4) Output: [
{
"surname": "xxx",
"forename": "jean",
"age": 24
},
{
"surname": "yyy",
"forename": "paul",
"age": 31
},
{
"surname": "zzz",
"forename": "julia",
"age": 28
},
{
"surname": "aaa",
"forename": "Paul",
"age": 40
}
]
Bubu93200 likes this post
Posts: 19
Threads: 5
Joined: Apr 2022
I'm busy turning a permanent storage list into an on-screen list, back in the day we would have said 'hard disk' instead of permanent storage.
Anyway, have you tried this?
# Python program to update
# JSON
import json
# JSON data:
x = '{ "organization":"GeeksForGeeks",
"city":"Noida",
"country":"India"}'
# python object to be appended
y = {"pin":110096}
# parsing JSON string:
z = json.loads(x)
# appending the data
z.update(y)
# the result is a JSON string:
print(json.dumps(z)) Produces this output:
.{“pin”: 110096, “organization”: “GeeksForGeeks”, “country”: “India”, “city”: “Noida”}
Posts: 8
Threads: 2
Joined: Apr 2022
(Apr-21-2022, 03:34 PM)deanhystad Wrote: Load json, modify and dump is the correct method. You do not want to append the file.
You should not worry about the format of your json data. Json files are easy to read, but are not designed to be human readable. You can make the file a little prettier using the indent argument.
import json
data = [
{"surname" : "xxx", "forename" : "jean", "age" : 24},
{"surname" : "yyy", "forename" : "paul", "age" : 31},
{"surname" : "zzz", "forename" : "julia", "age" : 28},
{"surname" : "aaa", "forename" : "Paul", "age" : 40}
]
with open("test.json", "w") as outfile:
json.dump(data, outfile, indent=4) Output: [
{
"surname": "xxx",
"forename": "jean",
"age": 24
},
{
"surname": "yyy",
"forename": "paul",
"age": 31
},
{
"surname": "zzz",
"forename": "julia",
"age": 28
},
{
"surname": "aaa",
"forename": "Paul",
"age": 40
}
]
Thanks a lot.
This output format isn't enough compact.
I prefer this one :
data = [
{"surname" : "xxx", "forename" : "jean", "age" : 24},
{"surname" : "yyy", "forename" : "paul", "age" : 31},
{"surname" : "zzz", "forename" : "julia", "age" : 28},
{"surname" : "aaa", "forename" : "Paul", "age" : 40}
]
Is it allowed for json format ?
If yes, I will add code to obtain this format (not with json library).
Is there other format to obtain this type of output? I can make my format but I would like to use known format.
Posts: 6,827
Threads: 20
Joined: Feb 2020
Apr-21-2022, 05:08 PM
(This post was last modified: Apr-21-2022, 05:08 PM by deanhystad.)
I do not think you can generate that output using json.dump(). If you want to know if it is allowed, try it yourself. Create a file using a text editor and try to load it.
menator01 likes this post
Posts: 8
Threads: 2
Joined: Apr 2022
(Apr-21-2022, 05:08 PM)deanhystad Wrote: I do not think you can generate that output using json.dump(). If you want to know if it is allowed, try it yourself. Create a file using a text editor and try to load it.
If I can load it with json library, it's allowed.
Ok. I'll try
Thanks for your help
Posts: 8
Threads: 2
Joined: Apr 2022
Apr-23-2022, 08:59 AM
(This post was last modified: Apr-23-2022, 08:59 AM by Bubu93200.)
(Apr-21-2022, 05:17 PM)Bubu93200 Wrote: (Apr-21-2022, 05:08 PM)deanhystad Wrote: I do not think you can generate that output using json.dump(). If you want to know if it is allowed, try it yourself. Create a file using a text editor and try to load it.
If I can load it with json library, it's allowed.
Ok. I'll try
Thanks for your help
(Apr-21-2022, 05:17 PM)Bubu93200 Wrote: (Apr-21-2022, 05:08 PM)deanhystad Wrote: I do not think you can generate that output using json.dump(). If you want to know if it is allowed, try it yourself. Create a file using a text editor and try to load it.
If I can load it with json library, it's allowed.
Ok. I'll try
Thanks for your help
I solved my problem.
I exhibit my code for other people who can have the same problem.
I found code to have :
- compact file
- file easily readable by human
- file uses append mode so its very quick to open et upgrade file. Its very usable for very very long files (log files)
- file is breakable in several files
- we can easily convert file to json file
- we can add index or datation on records (add keys as "time", "date", "index" if you want)
- records can be differents on each line
And to do this, I use JSONLINES format (I didn't know this format)
import jsonlines
def writeJsonlines(fileName, dictionary):
"""
https://jsonlines.org/
use import jsonlines
"""
try:
with jsonlines.open(fileName, "a") as jsonlinesfile:
jsonlinesfile.write(dictionary)
except IOError:
print("writeJsonlines : failed to open file")
def readJsonlines(fileName):
"""
https://jsonlines.org/
use import jsonlines
"""
try:
with jsonlines.open(fileName, 'r') as jsonlfile:
jsonl_list = list(jsonlfile)
print('readjsonlines : ', jsonl_list, "\n")
return jsonl_list
except IOError:
print("readJsonlines : failed to open file")
return None
File output is as :
Output: {"surname": "xxx", "forename": "jean", "age": 24},
{"surname": "yyy", "forename": "paul", "age": 31},
{"surname": "zzz", "forename": "julia", "age": 28},
{"surname": "aaa", "forename": "Paul", "age": 40},
{"name": "bruno", "age": 46}
Very usefull, flexible. great
|