DEV Community

loading...

CSV to JSON Converter with Go

fevziomurtekin profile image Fevzi Ömür Tekin ・3 min read

Files with a CSV extension are files that are written and saved in a specific order by separating the data with commas for database users. This type of file is needed when you want to transfer data in Excel. I will develop a program with the Go programming language, which can be converted and saved to.

Used CSV Files

## Used CSV Files
plaka,bolge,il,ilce
1,AKDENİZ,ADANA,ALADAĞ
1,AKDENİZ,ADANA,CEYHAN
1,AKDENİZ,ADANA,ÇUKUROVA
1,AKDENİZ,ADANA,FEKE
1,AKDENİZ,ADANA,İMAMOĞLU
......
......
......
3,EGE,AFYONKARAHİSAR,BAŞMAKÇI
3,EGE,AFYONKARAHİSAR,BAYAT
3,EGE,AFYONKARAHİSAR,BOLVADİN
3,EGE,AFYONKARAHİSAR,ÇAY
3,EGE,AFYONKARAHİSAR,ÇOBANLAR
3,EGE,AFYONKARAHİSAR,DAZKIRI
3,EGE,AFYONKARAHİSAR,DİNAR
3,EGE,AFYONKARAHİSAR,EMİRDAĞ
3,EGE,AFYONKARAHİSAR,EVCİLER
3,EGE,AFYONKARAHİSAR,HOCALAR
3,EGE,AFYONKARAHİSAR,İHSANİYE
3,EGE,AFYONKARAHİSAR,İSCEHİSAR
.....
.....
.....
.....
14,KARADENİZ,BOLU,DÖRTDİVAN
14,KARADENİZ,BOLU,GEREDE
14,KARADENİZ,BOLU,GÖYNÜK
14,KARADENİZ,BOLU,KIBRISCIK
14,KARADENİZ,BOLU,MENGEN
14,KARADENİZ,BOLU,MUDURNU
.....
.....
.....
.....
16,MARMARA,BURSA,GÜRSU
16,MARMARA,BURSA,HARMANCIK
16,MARMARA,BURSA,İNEGÖL
16,MARMARA,BURSA,İZNİK
16,MARMARA,BURSA,KARACABEY
16,MARMARA,BURSA,KELES
....
....
....
....
31,AKDENİZ,HATAY,DEFNE
31,AKDENİZ,HATAY,ARSUZ
31,AKDENİZ,HATAY,PAYAS
45,EGE,MANİSA,ŞEHZADELER
45,EGE,MANİSA,YUNUSEMRE
46,AKDENİZ,KAHRAMANMARAŞ,DULKADİROĞLU
46,AKDENİZ,KAHRAMANMARAŞ,ONİKİŞUBAT
47,GÜNEYDOĞU ANADOLU,MARDİN,ARTUKLU
.....
.....
.....
.....
63,GÜNEYDOĞU ANADOLU,ŞANLURFA,HALİLİYE
63,GÜNEYDOĞU ANADOLU,ŞANLIURFA,KARAKÖPRÜ
65,DOĞU ANADOLU,VAN,TUŞBA
65,DOĞU ANADOLU,VAN,İPEKYOLU
67,KARADENİZ,ZONGULDAK,KİLİMLİ
Enter fullscreen mode Exit fullscreen mode

Used Libraries

import (
  "encoding/csv"
  "encoding/json"
  "os"
  "strconv"
)

Enter fullscreen mode Exit fullscreen mode


Note: encoding/csv and encoding/json, csv to json converting Libraries.

Steps to Program

  • We are opening our .csv file first.

    src, err := os.Open("illerilceler.csv")
    if err != nil {
    panic(err)
    }
    defer src.Close()
    
  • The .json file was created.

dst, err := os.Create("il-ilce.json")
if err != nil {
  panic(err)
}
defer dst.Close()
Enter fullscreen mode Exit fullscreen mode
  • Then, we open the .csv file that we opened, and we save it to the .json file that we created
records := make([]Record, 0, len(rows)) // We are opening up a number of rows.
  for _, row := range rows {
    plaka, _ := strconv.ParseInt(row[0], 0, 64) // parsing.
    bolge := row[1]
    il := row[2]
    ilce := row[3]
    /*read the data in the first row and add it to the records recorder.*/
    records = append(records, Record{
      Plaka: plaka,
      Bolge: bolge,
      Il:    il,
      Ilce:  ilce,
    })
  }
  /*We're moving the recorder to json.*/
  err = json.NewEncoder(dst).Encode(records)
  if err != nil {
    panic(err)
  }
Enter fullscreen mode Exit fullscreen mode

All Codes

package main
import (
  "encoding/csv"
  "encoding/json"
  "os"
  "strconv"
)
type Record struct {
  Plaka int64
  Bolge string
  Il    string
  Ilce  string
  // High, Low, Close
}
func main() {
  src, err := os.Open("illerilceler.csv")
  if err != nil {
    panic(err)
  }
  defer src.Close()
  dst, err := os.Create("il-ilce.json")
  if err != nil {
    panic(err)
  }
  defer dst.Close()
  rows, err := csv.NewReader(src).ReadAll()
  if err != nil {
    panic(err)
  }
  records := make([]Record, 0, len(rows)) 
  for _, row := range rows {
    plaka, _ := strconv.ParseInt(row[0], 0, 64) // parseliyoruz.
    bolge := row[1]
    il := row[2]
    ilce := row[3]

    records = append(records, Record{
      Plaka: plaka,
      Bolge: bolge,
      Il:    il,
      Ilce:  ilce,
    })
  }

  err = json.NewEncoder(dst).Encode(records)
  if err != nil {
    panic(err)
  }
}
Enter fullscreen mode Exit fullscreen mode

Note : The codes of the project can be found at this address.

Discussion (0)

pic
Editor guide