0

I have a Python project and need to store multiple values ​​in JSON, but only the first value is stored. I tried to use a loop structure but still only the first value is stored. How can I store all values?

The code:

entry = {
'face_names': "any",
'data_atual': data_texto
 }

with open('consulte.json', 'w') as filehandle:
    json.dump(entry, filehandle)
  • 2
    What do you mean "only the first value is stored". Do you have the output value? – AlexandreS Nov 07 '19 at 13:07
  • Please clarify. Could you provide the list of values you want to store and the example of the desired output? – Max Voitko Nov 07 '19 at 13:08
  • 1
    please follow this link https://stackoverflow.com/questions/12994442/how-to-append-data-to-a-json-file – Avi Nov 07 '19 at 13:18
  • @AlexandreS i have this : { "data_atual": "07/11/2019 10:17", "face_names": "Funcionario"} – user9472526 Nov 07 '19 at 13:18
  • @MaxV i want store a default string "any" and the date. my desired output would be: {"data_atual": "07/11/2019 10:17", "face_names": "Funcionario}, {"data_atual":"07/11/2019 10:19", "face_names": "Funcionário"} – user9472526 Nov 07 '19 at 13:22

2 Answers2

1

Can you provide input values? But if you want to bind plural values to one key, then you should create a list of values and add it into the dict.

face_names = [<some_values_inside>]
entry['face_names'] = face_names
music_junkie
  • 189
  • 2
  • 16
  • It is a face recognition system, in case I need that each time the face is recognized it generates an entry in json, I print it in the list and it generates the values ​​I would like to store in json. the inputs are: current_date and face_names, the latter by default would be an "any" string – user9472526 Nov 07 '19 at 13:26
0

Assuming you want to store your data_texto object to JSON. So it's not possible if your object is not the type of let's say dict:

import json

data_texto = {
    '1': "1",
    '2': {'1': "2"}
}
entry = {
    'face_names': "any",
    'data_atual': data_texto
}

with open('consulte.json', 'w') as filehandle:
    json.dump(entry, filehandle)

Will output as:

{"face_names": "any", "data_atual": {"1": "1", "2": {"1": "2"}}}
Raymond Reddington
  • 1,709
  • 1
  • 13
  • 21