If I did not get it wrong, I think what you want to do is add a new line with another JSON object to an existing file.
I mean, I guess you have something like this:
datos = funcion_que_genera_diccionario_python()
# Ahora datos es un diccionario python que contiene por ejemplo
# {"mensaje": "hola", "miID": "123", "reply": "hola"}
# Abrimos fichero en modo "a" para añadir
with open("fichero.json", "a") as f:
# Convertimos en cadena el diccionario python
cadena = json.dumps(datos)
# Y la añadimos al fichero
f.write(cadena)
# Retorno de carro
f.write("\n")
If you run that several times, the fichero.json
would have something like this:
{"mensaje": "hola", "miID": "123", "reply": "hola"}
{"mensaje": "hola", "miID": "123", "reply": "hola"}
{"mensaje": "hola", "miID": "123", "reply": "hola"}
If that is your goal, the previous example should do what you expect. As you will see in the json file, the quotes around each line are not saved as you mentioned in the question.
However I must warn you that this file is not a valid JSON, it contains several objects and a valid JSON must contain only one (although that one could be a list in which there is more).
That is, if you try to read the previous file as if it were a JSON like this:
with open("fichero.json") as f:
datos = json.load(f)
You will get the error
json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 53)
because once he has read the first object he does not wait anymore, but finds more.
If you want to be able to re-read as JSON what you have uploaded, what you have to do is that that file save a list instead of a series of dictionaries. Something like this:
datos = funcion_que_genera_diccionario_python()
# Leemos del fichero JSON todos los diccionarios que teníamos
# guardados de ejecuciones anteriores, que serán una lista
with open("fichero.json") as f:
anteriores = json.loads(f)
# Añadimos el nuevo diccionario a la lista anterior
anteriores.append(datos)
# Volvemos a abrir el fichero y volcamos la nueva lista
with open("fichero.json", "w") as f:
json.dump(anteriores, f)
To work properly, the fichero.json
should exist before running the program for the first time, and contain an empty list, like this:
[]
After several executions, the file would contain something like this (I format it so it looks better, but it would probably be all stuck in a single line):
[
{"mensaje": "hola", "miID": "123", "reply": "hola"},
{"mensaje": "hola", "miID": "123", "reply": "hola"},
{"mensaje": "hola", "miID": "123", "reply": "hola"}
]
This is a valid JSON, since it is a single object: a list that has several dictionaries inside.
Note on efficiency
The previous approach guarantees that the fichero.json
always has valid JSON, but at the cost of each time the program is executed it takes longer to generate that file (since it must read and process what it had before, add another dictionary) and turn everything over again.)
The first approach that simply added lines to the file when opening it in "a" mode was much more efficient, but it had the disadvantage that what it generated was not valid JSON, so you could not read it with json.load()
.
A possible intermediate solution is to use the "append" mode as in the first part of the answer, but when you go to read the file, do not try to do it in a single json.load()
, which will fail, but process it line by line and each line (which would be a string with a valid JSON) will parse it with json.loads()
.
So for example:
todos_los_json = []
with open("fichero.json") as f:
for linea in f:
todos_los_json.append(json.loads(lineea))
At the end of the loop you will have a list in which each element is a python dictionary.