Alex T
Alex T

Reputation: 21

Looking to convert a json file into a csv file using Golang

I am trying to take the data in the "carrier.json" file and create a CSV file using Go with the information. However, I am receiving the follow error/notice

./carrier_json.go:53:48: obj.APIVersion undefined (type Json has no field or method APIVersion)
./carrier_json.go:53:69: obj.CarrierSid undefined (type Json has no field or method CarrierSid)
./carrier_json.go:53:85: obj.AccountSid undefined (type Json has no field or method AccountSid)
./carrierlookup.go:12:6: main redeclared in this block
    previous declaration at ./carrier_json.go:31:6

I have searched online for several hours and any help would be much appreciated. Note, I am not very technical.

The "carrier.json" has the following,

{
    "Message360": {
        "ResponseStatus": 1,
        "Carrier": {
            "ApiVersion": "3",
            "CarrierSid": "c7e57a2a-92d7-0430",
            "AccountSid": "2f4ce81a-f08d-04e1",
            "PhoneNumber": "+19499999999",
            "Network": "Cellco Partnership dba Verizon Wireless - CA",
            "Wireless": "true",
            "ZipCode": "92604",
            "City": "Irvine",
            "Price": "0.0003",
            "Status": "success",
            "DateCreated": "2017-10-13 18:44:32"
        }
    }
}

I have a go file called carrier_json.go that has the following information.

package main

import (
    "encoding/csv"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "os"
    "strconv"
)

type Json struct {
    Message360 struct {
        ResponseStatus int `json:"ResponseStatus"`
        Carrier        struct {
            APIVersion  string `json:"ApiVersion"`
            CarrierSid  string `json:"CarrierSid"`
            AccountSid  string `json:"AccountSid"`
            PhoneNumber string `json:"PhoneNumber"`
            Network     string `json:"Network"`
            Wireless    string `json:"Wireless"`
            ZipCode     string `json:"ZipCode"`
            City        string `json:"City"`
            Price       string `json:"Price"`
            Status      string `json:"Status"`
            DateCreated string `json:"DateCreated"`
        } `json:"Carrier"`
    } `json:"Message360"`
}

func main() {
    // reading data from JSON File
    data, err := ioutil.ReadFile("carrier.json")
    if err != nil {
        fmt.Println(err)
    }
    // Unmarshal JSON data
    var d []Json
    err = json.Unmarshal([]byte(data), &d)
    if err != nil {
        fmt.Println(err)
    }
    // Create a csv file
    f, err := os.Create("./carrier.csv")
    if err != nil {
        fmt.Println(err)
    }
    defer f.Close()
    // Write Unmarshaled json data to CSV file
    w := csv.NewWriter(f)
    for _, obj := range d {
        var record []string
        record = append(record, strconv.FormatInt(obj.APIVersion, 10), obj.CarrierSid, obj.AccountSid)
        w.Write(record)
        record = nil
    }
    w.Flush()
}

Upvotes: 1

Views: 972

Answers (1)

littlepoint
littlepoint

Reputation: 124

There are several mistakes.

  1. carrier.json is an object instead of an array, but you want to Unmarshal it into []Json
  2. the 1st parameter of function strconv.FormatInt is of type int64, but APIVersion is of type string
  3. obj is of type Json, which does not have an element called APIVersion. it should be obj.Message360.Carrier.APIVersion
  4. I suppose you want to want to write multiple CSV records since d is a slice of Json. You should use WriteAll instead of Write, because Write only writes a single CSV record. (improvement: add head for csv file)

Upvotes: 1

Related Questions