-1

hi i want to create a json file but when I print the result the result is complete but when I save in a file .json the file only save the last key of the data

import json
import csv


jsonFile = r'Prueba.json'

with open('file1.csv', newline='') as csvfile:
    datos = csv.DictReader(csvfile)
    for row in datos:
        datos_jason = json.dumps(row, indent=4, skipkeys = True)
        print(datos_jason)

        with open(jsonFile, 'w', encoding='utf-8') as jsonf:
            jsonf.write(json.dumps(row, indent=4))

in the terminal

{
"companyemail": "user 1",
"password": "12345",
"firstname": "Don10",
"lastname": "Api10"
}
{
"companyemail": "user 2",
"password": "12345",
"firstname": "Don11",
"lastname": "Api11"
}
{
"companyemail": "user 3",
"password": "12345",
"firstname": "Don12",
"lastname": "Api12"
}
{
"companyemail": "user 4",
"password": "12345",
"firstname": "Don13",
"lastname": "Api13"
}
{
"companyemail": "user 5",
"password": "12345",
"firstname": "Don14",
"lastname": "Api14"
}

but in the json file

{
"companyemail": "user 5",
"password": "12345",
"firstname": "Don14",
"lastname": "Api14"
}

In the file only save the last users

I add the csv file

companyemail,password,firstname,lastname
user 1,12345,Don10,Api10
user 2,12345,Don11,Api11
user 3,12345,Don12,Api12
user 4,12345,Don13,Api13
user 5,12345,Don14,Api14

I'm new in programming and I want to push this json format to a POST with API, those part work ok but only upload the last user

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
  • Does this answer your question? [How do I append to a file?](https://stackoverflow.com/questions/4706499/how-do-i-append-to-a-file) – Henry Woody Jan 11 '23 at 17:55
  • Or better is to just write all of `datos` to a file in one go instead of line by line (which produces invalid JSON anyway). – Henry Woody Jan 11 '23 at 17:56

1 Answers1

1

If you want a JSON file with one list of objects, you can just read the whole CSV reader into a single list, then dump that:

import json
import csv

with open("file1.csv") as csvfile:
    with open(r"Prueba.json", "w", encoding="utf-8") as jsonf:
        data = list(csv.DictReader(csvfile))
        json.dump(data, jsonf, indent=4)

If you want a JSONL/ND-JSON file, e.g. one JSON document per line,

import json
import csv

with open("file1.csv") as csvfile:
    with open(r"Prueba.json", "w", encoding="utf-8") as jsonf:
        for line in csv.DictReader(csvfile):
            print(json.dumps(line), file=jsonf)
AKX
  • 152,115
  • 15
  • 115
  • 172
  • the problem is the file si saved in this format ,,, [ { "companyemail": "user 1", "password": "12345", "firstname": "Don10", "lastname": "Api10" }, { "companyemail": "user 2", "password": "12345", "firstname": "Don11", "lastname": "Api11" }, { "companyemail": "user 3", "password": "12345", "firstname": "Don12", "lastname": "Api12" }, ] ,,, i no need [ ] this only start like the example – Mauricio Takano Jan 11 '23 at 18:25
  • That won't be a valid JSON file. – AKX Jan 11 '23 at 18:35